如何使用.htaccess将sub.example.com重定向到www.example.com?

时间:2017-08-29 16:11:27

标签: .htaccess url url-rewriting apache2

我想将sub.example.com重定向到www.example.com。我使用了重写.htaccess。我写了这个:

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]

你知道我在这种情况下如何编写我的重写规则吗?

1 个答案:

答案 0 :(得分:1)

如果您指定域名,则可以对两种情况使用一个规则:

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