如果url不包含特定目录,则htaccess重定向

时间:2014-02-11 02:40:10

标签: .htaccess mod-rewrite redirect

使用htaccess我需要将所有不包含特定URL路径(医疗保健)的URL重定向到新域,同时保持原始URL不变。

示例:

healthcare.domain.com/anydirectory/anything.html

应该重定向到

staging.domain.com/anydirectory/anything.html

但不应重定向包含/ healthcare的任何网址。与healthcare.domain.com/industries/healthcare/customers.html一样,不应重定向。

两个域都位于同一服务器上,healthcare.domain.com指向与staging.domain.com相同的根。他们希望能够在healthcare.domain.com上访问以下页面staging.domain.com/industries/healthcare/。我将healthcare.domain.com设置为一个新的子域,将其根目录指向与staging.domain.com相同的根。然后我在医疗保健.domain.com的htaccess中设置了重定向,以重定向到healthcare.domain.com/industries/healthcare,它完美无缺,但如果您点击/ industries / healthcare /之外的网站上的任何其他地方,我需要带用户返回staging.domain.com/whatever

这是我到目前为止所做的:

RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.healthcare\.domain\.com$
RewriteRule ^/?$ "http\:\/\/healthcare\.domain\.com\/industries\/healthcare\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/ [NC]
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R=302]

但上述内容始终会返回http://staging.domain.com/index.php

2 个答案:

答案 0 :(得分:11)

尝试:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R]

答案 1 :(得分:1)

您可以使用此代码:

RewriteCond %{HTTP_HOST} ^(www\.)?healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/ [NC]
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R=302]

RewriteCond %{HTTP_HOST} ^(www\.)?healthcare\.domain\.com$ [NC]
RewriteRule ^/?$ http://healthcare.domain.com/industries/healthcare/ [R=302,L]