htaccess将所有域重定向到一个域,但一个域除外

时间:2017-11-30 08:39:42

标签: apache .htaccess redirect mod-rewrite

有几个(很多)域需要重定向到一个主域。 现在它完成了这段代码:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.maindomain.com$ [NC]
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [L,R=301]

但现在网站空间需要收听第二个域名。因此,第二个域(以及所有子域)需要从上面的规则中排除。

更多例子:

www.maindomain.com        => no redirect
maindomain.com            => www.maindomain.com
*.maindomain.com          => www.maindomain.com
domain.com                => www.maindomain.com
www.domain.com            => www.maindomain.com
domain-something.com      => www.maindomain.com
www.domain-something.com  => www.maindomain.com

www.second-maindomain.com => no redirect
second-maindomain.com     => www.second-maindomain.com
*.second-maindomain.com   => www.second-maindomain.com

1 个答案:

答案 0 :(得分:0)

这应该做:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !www.maindomain.com$ [NC]
RewriteCond %{HTTP_HOST} !second-maindomain.com$ [NC]
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !maindomain.com$ [NC]
RewriteCond %{HTTP_HOST} !www.second-maindomain.com$ [NC]
RewriteRule ^(.*)$ http://www.second-maindomain.com/$1 [L,R=301]