Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

301 redirection using .htaccess problems

3:57 pm on Jun 11, 2013 (gmt 0)

New User

5+ Year Member

joined:Apr 20, 2008
votes: 0

We all know we can use .htaccess to 301 a website abc.com to cde.com

Now my situation is:

I pucahsed a shared hosting, /public_html/ is abc.com 's root, /public_html/www.cde.com/ is cde.com's root folder.

Whenever I changed .htacess of abc.com 's, it will affect cde.com.

for example, I put the following code to /public_html/.htaccess to redirect all the urls from abc.com to cde.com, but after I do so, when I type abc.com/test.php, it redirects to cde.com/test.php no problem, but gives a 404 error, for now all the links from cde.com is dead and unacessable. When i delete the following code from .htacess, cde.com works fine again.

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://cde.com/

My question is, how to change /public_html/.htaccess to redirect abc.com's url's to cde.com and don't affect cde.com website running.

Thank you for yoru help.
4:53 pm on June 11, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
votes: 0

You'd want to add a condition to your rewrite rule.

Something like this:

RewriteCond %{HTTP_HOST} !^cde\.com$
RewriteRule (.*) http://cde.com/$1 [R=301,L]

Bear in mind, that any subsequent domains you attach to this file structure will be similarly 301'd. If that was undesired, you would need to modify the condition so that is only 301'd from abc.com
RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$
RewriteRule (.*) http://cde.com/$1 [R=301,L]
Or you would need to add additional exclusionary rules for the new domains:
RewriteCond %{HTTP_HOST} !^cde\.com$
RewriteCond %{HTTP_HOST} !^(www\.)?efg\.com$
RewriteRule (.*) http://cde.com/$1 [R=301,L]

Also of note - the first and third solutions in this post will force requests to
to instead go to
, which prevents a potential problem with duplicate content arising.
8:03 pm on June 11, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
votes: 244

Do your additional domains live in subdirectories inside the first domain's directory? Ugh. Out of your control, I know, but it's definitely easier when all domains are parallel so each can have its own htaccess without worrying about the others.

You've got at least two different issues:

#1 any rule specific to a single domain needs a preceding RewriteCond that says either

RewriteCond %{HTTP_HOST} !abc\.com


RewriteCond %{HTTP_HOST} def\.com

whichever is more convenient. Note that you have to leave off the opening anchor because the rule has to cover requests with or without leading www and maybe even subdomains.

#2 domain-name canonicalization. Here each rule needs two conditions, like this:

RewriteCond %{HTTP_HOST} example\.com
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

And then there's

#3 well, I did say "at least two" the all-purpose "index.html" redirect, which will be the same on all domains everywhere. Here it is tempting to capture the %{HTTP_HOST} and simply recycle it because then you don't need to write out a separate rule for each domain. But if you do this, you risk having to do two separate redirects if the request was for
and you need to redirect to

You may be similarly tempted to take any rules specific to the new domains and make a new htaccess in their individual directories. Unfortunately this gets VERY messy if you have RewriteRules in both places. The two options are:

RewriteOptions inherit
= RewriteRules in the outer htaccess will be put on hold while the second htaccess is checked, and will only kick in if not superseded by something in the inner htaccess

(no RewriteOptions inherit)
= RewriteRules in the outer htaccess will simply be IGNORED; only the inner htaccess will apply

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members