.htaccess不会重定向子域

时间:2014-07-09 22:51:07

标签: wordpress apache .htaccess mod-rewrite redirect

我已设置.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>

为什么第二个cond /规则没有生效?

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

有关您的信息,当您重定向到顶级域名时,您会犯错误(除非您拥有巨大的带宽):为无前缀域设置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]