2014-02-11 91 views
7

使用htaccess我需要將所有不包含特定URL路徑(保健)的URL重定向到新域,同時保持原始URL不變。如果URL不包含特定目錄,htaccess重定向

例子:

healthcare.domain.com/anydirectory/anything.html

應該重定向到

staging.domain.com/anydirectory/anything.html

但任何網址含有/醫療保健不應該被重定向。正如在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相同的根目錄。然後,我在htaccess中爲healthcare.domain.com設置重定向,以重定向到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

+0

是'healthcare.domain.com'和相同的主機和同'VirtualHost'上'staging.domain.com'? – anubhava

+0

我在上面的問題中添加了更多以回答您的問題。 – OneSolutionStudio

+0

「staging.domain.com/industries/healthcare /」與「healthcare.domain.com/industries/healthcare /」相同嗎? – anubhava

回答

9

嘗試:

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

我有一個和你的幾乎完全相同的,它返回了相同的結果 - 它只是給了我staging.domain.com/index.php。我在上面的問題中添加了更多信息,這可能會爲我們提供更多信息。 – OneSolutionStudio

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] 
相關問題