1

我需要编写一个重写规则来从一个域重定向到另一个域。

   maindomain.com/en/ -> secounddomain.com/en/
   maindomain.com/en/pagename.html -> secounddomain.com/en/pagename.html
   www.maindomain.com/en/ -> secounddomain.com/en/
   www.maindomain.com/en/pagename.html -> secounddomain.com/en/pagename.html
   secounddomain.com -> secounddomain.com/en/
   www.secounddomain.com -> secounddomain.com/en/

并且为了安全(没有背部)

   secounddomain.com/pl/ -> secounddomain.com/en/
   www.secounddomain.com/pl/ -> secounddomain.com/en/

我尝试这样做,但它不起作用:

RewriteCond %{HTTP_HOST} ^maindomain.com/en/$ [NC]
   RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]

   RewriteCond %{HTTP_HOST} ^www.maindomain.com/en/$ [NC]
   RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]

   RewriteCond %{HTTP_HOST} ^www.secounddomain.com/pl/$ [NC]
   RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]

   RewriteCond %{HTTP_HOST} ^secounddomain.com/pl/$ [NC]
   RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]

此致。

4

1 回答 1

1

首先这个条件是错误的:

RewriteCond %{HTTP_HOST} ^maindomain.com/en/$ 

因为%{HTTP_HOST}只能匹配主机名,因此它只能匹配maindomain.com

这是您的 .htaccess 的外观:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteRule ^(en(?:/.*|))$ http://secounddomain.com/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^(www\.)?secounddomain\.com$ [NC]
RewriteRule (?!^en/)^(.*)$ http://secounddomain.com/en/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^(www\.)?secounddomain\.com$ [NC]
RewriteRule ^pl(/.*|)$ http://secounddomain.com/en$1 [L,R=301,NC]
于 2013-08-01T10:44:49.980 回答