Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect if the url does not contain a specific directory

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.

Example:

healthcare.domain.com/anydirectory/anything.html

should redirect to

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.

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

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

like image 303
OneSolutionStudio Avatar asked Feb 11 '14 02:02

OneSolutionStudio


People also ask

How to redirect a page to another page using htaccess?

Make sure that the opening of the .htaccess file contains the 2 lines of code below which enables the Apache module to rewrite the URLS, then place your redirections below them When you need to switch a website from an old domain to a new domain, you need to redirect all your page URLs, this is when htaccess is your friend.

How to redirect HTTP requests to another URL in PHP?

Open .htaccess file Open terminal and run the following command to open .htaccess file 3. Redirect if URL contains If you want to redirect all requests which contain the word ‘data’ in them then add the following lines to your .htaccess file. RewriteCond % {REQUEST_URI} data RewriteRule .* index.php

How to redirect the contents of a directory to another directory?

To redirect the contents of a whole directory to the webserving root: To redirect the contents of a subdirectory to another domain but in the same subdirectory Make sure that the opening of the .htaccess file contains the 2 lines of code below which enables the Apache module to rewrite the URLS, then place your redirections below them

How to redirect if URL contains specific string or word?

Sometimes you may need to redirect if URL contains a specific word or string in requested URL. You can easily do this using .htaccess file in Apache web server. In this article, we will look at how to redirect if URL contains certain string or word.


2 Answers

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]
like image 113
anubhava Avatar answered Sep 22 '22 16:09

anubhava


Try:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R]
like image 45
Jon Lin Avatar answered Sep 19 '22 16:09

Jon Lin