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

时间:2022-09-25 10:38:49

With htaccess I need to redirect all URLs that don't contain a specific URL path (healthcare) to a new domain while keeping the original URL intact.

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

Example:

例:

healthcare.domain.com/anydirectory/anything.html

healthcare.domain.com/anydirectory/anything.html

should redirect to

应该重定向到

staging.domain.com/anydirectory/anything.html

staging.domain.com/anydirectory/anything.html

but any URL containing /healthcare should not get redirected. As in healthcare.domain.com/industries/healthcare/customers.html should not be redirected.

但是任何包含/ healthcare的URL都不应该被重定向。与healthcare.domain.com/industries/healthcare/customers.html一样,不应重定向。

Both domains are on the same server and healthcare.domain.com is pointed to same root as staging.domain.com. They wanted to be able to access the following page staging.domain.com/industries/healthcare/ at healthcare.domain.com. I setup healthcare.domain.com as a new subdomain pointing its root to the same root as staging.domain.com. I then setup a redirect in the htaccess for healthcare.domain.com to redirect to healthcare.domain.com/industries/healthcare which works perfectly, but if you click anywhere else on the site outside of /industries/healthcare/ I need to bring the user back to staging.domain.com/whatever

两个域都位于同一服务器上,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

Here is what I have so far:

这是我到目前为止:

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]

But the above always returns http://staging.domain.com/index.php

但以上总是返回http://staging.domain.com/index.php

2 个解决方案

#1


11  

Try:

尝试:

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

#2


1  

You can use this code:

您可以使用此代码:

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]

#1


11  

Try:

尝试:

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

#2


1  

You can use this code:

您可以使用此代码:

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]