Htaccess redirects if the url does not contain a specific directory

With htaccess, I need to redirect all URLs that do not contain a specific URL path (healthcare) to the new domain, keeping the original URL intact.

Example:

healthcare.domain.com/anydirectory/anything.html

should redirect to

staging.domain.com/anydirectory/anything.html

but any url containing / healthcare should not be redirected. Like healthcare.domain.com/industries/healthcare/customers.html should not be redirected.

Both domains are on the same server, and healthcare.domain.com points to the same root as staging.domain.com. They wanted to have access to the next page staging.domain.com/industries/healthcare/in healthcare.domain.com. I install healthcare.domain.com as a new subdomain pointing its root to the same root as staging.domain.com. Then I set up redirection in htaccess for healthcare.domain.com to redirect to healthcare.domain.com/industries/healthcare, which works fine, but if you click somewhere else on a site outside of / industries / healthcare / I need to bring a user returns to staging.domain.com/whatever

Here is what I still have:

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

+7
redirect .htaccess mod-rewrite
source share
2 answers

Try:

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

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
source share

All Articles