2017-08-29 21 views
1

我想將sub.example.com重定向到www.example.com。我使用了.htaccess的重寫。我寫這個:如何使用.htaccess將sub.example.com重定向到www.example.com?

RewriteCond %{HTTP_HOST} ^sub.example.+$ [NC] 
RewriteRule^http://www.example.com/$1 [R=301,L] 

我已經激活重寫:sudo a2enmod rewrite。

而且我已經重寫的example.com www.example.com有:

RewriteCond %{HTTP_HOST} ^example.+$ [NC] 
RewriteRule^http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

你知道我怎麼也得寫我重寫規則,在這種情況下?

+0

到底是什麼問題?這些規則應該沒問題,就像做你想做的事一樣。我看到的唯一可能的問題是'RewriteRule^http://www.example.com/$1 [R = 301,L]'沒有捕獲組,應該是'RewriteRule ^(。*)$ http: //www.example.com/$1 [R = 301,L]' – Charles

+0

當我做了「curl -I http://sub.example.com」時,URL不會重定向到www.example.com – camilleB

回答

1

如果指定域名,你可以使用一個規則,對於兩種情況:

RewriteCond %{HTTP_HOST} ^(sub\.)?example.+$ [NC] 
RewriteRule^http://www.exemple.com%{REQUEST_URI} [R=301,L] 
相關問題