【问题标题】:.htaccess isn't redirecting subdomain.htaccess 没有重定向子域
【发布时间】:2014-07-09 22:51:07
【问题描述】:

我已经设置了一个 .htccess 文件来将 www 重定向到非 www 并删除尾部斜杠。我还必须确保business.domain.com 也将(301)重定向到domain.com。除了重定向business.domain.com,其他规则都有效。

<IfModule mod_rewrite.c>
RewriteEngine On

# redirect to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# redirect business subdomain to no subdomain
RewriteCond %{HTTP_HOST} ^business\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# redirect non-trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

任何想法为什么第二个条件/规则没有生效?

【问题讨论】:

  • 愚蠢的是,我刚刚意识到业务子目录中存在一个 .htaccess 文件。将其重命名为 .htaccess_,现在可以使用了。

标签: wordpress apache .htaccess mod-rewrite redirect


【解决方案1】:

您可能会遇到尾部斜杠。怎么样:

# redirect business subdomain to no subdomain
RewriteCond %{HTTP_HOST} ^business\.(.+)/?$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]

或者只是

# redirect business subdomain to no subdomain
RewriteCond %{HTTP_HOST} ^business\. [NC]
RewriteRule ^ http://domain.com%{REQUEST_URI} [NE,R=301,L]

【讨论】:

    【解决方案2】:

    请注意,当您重定向到顶级域时(除非您有巨大的带宽),您犯了一个错误:为无前缀域设置 cookie 会将它们设置为跨所有子域...和会话 总是 设置 cookie。

    无论如何,这里有一条对我来说完美无缺的规则:

    # If only two groups separated by a '.':
    RewriteCond %{HTTP_HOST} ^mywebsite\.(fr|com|net|org|eu) [NC]
    # This means there's no www => force redirection:
    RewriteRule (.*) http://www.mywebsite.%1$1 [QSA,R=301,L]
    

    我也有相反的情况:

    # "business" => no subdomain:
    RewriteCond %{HTTP_HOST} ^(business\.)mywebsite\.(fr|com|net|org|eu) [NC]
    RewriteRule (.*) http://mywebsite.%2$1 [QSA,R=301,L]
    

    【讨论】:

      猜你喜欢
      • 2013-11-12
      • 1970-01-01
      • 2013-02-18
      • 2019-10-13
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多