【问题标题】:.htaccess www - redirect (R301) with dynamic domainname.htaccess www - 使用动态域名重定向 (R301)
【发布时间】:2017-08-26 14:21:04
【问题描述】:

我想重定向 3 级或 4 级域以添加“www.”:

DirectoryIndex index.php

RewriteEngine on
RewriteRule ^$ /home/ [R=301,L]


RewriteRule ^(?!www[\.]) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=/$1 [QSA] 

我的问题是它可能是不同数量的级别...... 万维网。弗罗施科尼格84。放置。网 万维网。弗罗施科尼格84。去

所以当它不是以“www”开头时。然后添加,否则不...

重写规则 ^(?!www[\.]) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

但它不起作用,......它每次都在添加......所以它的结尾就像......

如何检查它是否以“www”开头。或不? 如果可能,那么没有单独的条件。 :)

我也为 .NET (web.config) 创建了类似的重写规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <rewrite>
   <rules>
    <rule name="RedirectToHome" stopProcessing="true">
     <match url="^$" />
     <conditions trackAllCaptures="true">
      <add input="{CACHE_URL}" pattern="^(https?)://" />
     </conditions>
     <action type="Redirect" redirectType="Permanent" url="{ToLower:{C:1}://{HTTP_HOST}/home/}" />
    </rule>
    <rule name="RedirectToWWW" stopProcessing="true">
     <match url="(.*)" ignoreCase="false"  />
     <conditions>
      <add input="{CACHE_URL}" pattern="^(https?://)(?!www[\.])" />
     </conditions>
     <action type="Redirect" url="{ToLower:{C:1}www[\.]{R:1}}" redirectType="Permanent" />
    </rule>
    <rule name="RewriteToParams" stopProcessing="true">
     <match url="^(.*)$" ignoreCase="false" />
     <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      <add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
     </conditions>
     <action type="Rewrite" url="{ToLower:index.php?p=/{R:1}}" appendQueryString="true" />
    </rule>
   </rules>
  </rewrite>
 </system.webServer>
</configuration>

:/

【问题讨论】:

    标签: .htaccess redirect web


    【解决方案1】:

    域名不是 RewriteRule 中测试 uri 的一部分。

    你可以使用:

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    

    【讨论】:

      【解决方案2】:

      解决方案:

      DirectoryIndex index.php
      
      RewriteEngine on
       #RedirectToHome
        RewriteRule ^$ /home/ [R=301,L]
      
       #RedirectToWWW
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
      
       #RewriteToParams
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?p=/$1 [QSA]
      

      【讨论】:

        猜你喜欢
        • 2014-06-27
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 2018-03-27
        • 2015-03-12
        • 2015-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多