301在.htaccess文件中重定向多个域名

时间:2015-03-12 22:06:36

标签: .htaccess redirect rewrite

我试图为我拥有的某些域进行301重定向。我有多个域指向我的主域。

我目前正在使用以下代码重定向一个域。但是,将多个域指向同一主域的最佳方法是什么?

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain-name.me.uk [NC]
rewriterule ^(.*)$ http://www.main-domain.co.uk/$1 [R=301,L]

这是执行多个域的正确方法吗?

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain-name.me.uk [NC]
rewritecond %{http_host} ^domain-name.digital [NC]
rewriterule ^(.*)$ http://www.main-domain.co.uk/$1 [R=301,L]

1 个答案:

答案 0 :(得分:0)

我建议使用基于否定的条件来避免匹配多个主机名并避免多个条件。

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} !^(www\.)?main-domain\.co\.uk$ [NC]
RewriteRule ^ http://www.main-domain.co.uk%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^(www\.)?main-domain\.co\.uk$将确保在当前请求不是www.main-domain.co.uk时重定向。

相关问题