2014-07-09 113 views
1

我已經設置了一個.htccess文件來將www重定向到非www,並且還刪除了尾部斜線。我也必須確保business.domain.com也重定向(301)到domain.com。除重定向business.domain.com之外的其他規則。.htaccess沒有重定向子域

<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/rule沒有生效?

+0

愚蠢我剛剛意識到.htaccess文件是存在於業務子目錄。將其重命名爲.htaccess_,現在可以使用。 – iamjonesy

回答

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] 
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]