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
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.
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
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
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.
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]
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^healthcare\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/healthcare/
RewriteRule ^(.*)$ http://staging.domain.com/$1 [L,R]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With