0

我需要一种方法来重定向所有且仅以文件夹开头的 URL:“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]

但它不起作用,因为它将所有 url 从 example.com 重写为 jp.example.com

4

2 回答 2

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]
于 2013-11-03T04:18:13.553 回答
0

您只需要检查请求的 URI 是否以/homeor开头/car,如下所示:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^jp.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^(/home|/car)(/.*)?$ [NC]
RewriteRule ^(.*)$ http://jp.example.com/$1 [R=301]
于 2013-11-02T18:43:47.313 回答