2013-11-02 22 views
0

我需要一种方法来重定向所有和只有从文件夹开始的网址:“home /”“car /”,从example.com到jp.example.com。如何仅在这些情况下重写?

因此,例如:

example.com/home/test.html ---> jp.example.com/home/test.html 
example.com/car/test.html ---> jp.example.com/car/test.html 
jp.example.com/home/second-test.html ---> jp.example.com/home/second-test.html 
jp.example.com/third-test.html ---> example.com/third-test.html 

我使用这个代码:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^jp.example.com$ 
RewriteRule ^(.*)$ http://jp.example.com/$1 [R=301] 

但由于它重写来自example.com的所有URL来jp.example.com

这是行不通的

回答

0

您只需检查URI请求是以/home还是/car开头,如下所示:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^jp.example.com$ [NC] 
RewriteCond %{REQUEST_URI} ^(/home|/car)(/.*)?$ [NC] 
RewriteRule ^(.*)$ http://jp.example.com/$1 [R=301] 
0

您可以使用这些规则:

RewriteEngine on 

# conditions 1 & 2 
RewriteCond %{HTTP_HOST} ^(www\.)?(example\.com)$ [NC] 
RewriteRule ^(home|care)/ http://jp.%1%{REQUEST_URI} [L,NC,NE,R=301] 

# conditions 3 
RewriteCond %{HTTP_HOST} ^jp\.example\.com$ [NC] 
RewriteRule ^(home)/second_(test\.html)$ /$1/$2 [L,NC,R=301] 

# conditions 4 
RewriteCond %{HTTP_HOST} ^jp\.(example\.com)$ [NC] 
RewriteRule ^(third-test\.html)$ http://%1/$1 [L,NC,R=301]