Redirect loop from two domain using the same .htaccess

Redirect loop from two domain using the same .htaccess - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, 301-redirect, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


How our FTP was setup, the root level is Website #1. Website #2 has it's own directory folder inside that root.



Both websites refer to the same .htaccess.



If I do Redirect 301 /example/ http://www.website2.com/example/, there will be a redirect loop.



It will redirect http://www.website1.com/example/ to http://www.website2.com/example/.



Then also redirect http://www.website2.com/example/ to http://www.website2.com/example/
(which creates the redirect loop)



What I want is to somehow only redirect http://www.website1.com/example/ to http://www.website2.com/example/ without creating the redirect loop.



NOTE: I do NOT want every webpage on Website#1 to redirect to Website#2. Just that specific URL to the new URL.


Solution :

You'll need to use mod_rewrite (as opposed to a mod_alias Redirect) and check the HTTP_HOST server variable (which tells you which site has been accessed). Something like the following at the top of your .htaccess file:



RewriteEngine On
RewriteCond %HTTP_HOST ^(www.)?website1.com$ [NC]
RewriteRule ^example/(.*)$ http://www.website2.com/example/$1 [R=302,L]


Change the 302 (temporary) to a 301 (permanent) redirect if you need a permanent redirect, but only after you have tested to make sure it's working OK. (301 redirects are cached by the browser so can make testing problematic - unless you test with the browser cache disabled.)



This redirects /example/<something> to http://www.website2.com/example/<something>, in much the same way as the original Redirect directive would do (which is prefix matching).






UPDATE: To redirect just the homepage, ie. http://www.website1.com/ to http://www.website2.com/, you can use something like the following:



RewriteCond %HTTP_HOST ^(www.)?website1.com$ [NC]
RewriteRule ^$ http://www.website2.com/ [R=302,L]


Note the RewriteRule pattern ^$ - this matches the empty URL (ie. the homepage only). (Note that the URL matched by the RewriteRule pattern is less the directory-prefix.)


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect