htaccess重定向到另一个域和语言

时间:2013-08-01 09:00:23

标签: .htaccess mod-rewrite url-rewriting rewrite

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

   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]

最好的问候。

1 个答案:

答案 0 :(得分:0)

首先,这种情况是错误的:

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]