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



我想将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]

你知道在这种情况下我必须如何写"重写"规则吗?

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

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

相关内容

最新更新