View Full Version : htaccess 301 domain redirect not quite working

09-27-2011, 01:49 PM
OK, I have two domains, let’s say example1.com and example2.com. Both are accessing the same webspace. Domain 1 used to be the default domain for years, now I want to make domain 2 the default and redirect domain 1 to domain 2. Additionally, I had a www-to-non-www redirection in place for domain1 and of course I want to keep this (for the new domain and the redirection from old to new).

I have the following rule in my htaccess file:

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

Now, this is correctly redirecting both, http://www.example1.com and http://www.example2.com to http://example2.com but it’s not redirecting http://example1.com (without www) to domain 2.

How can I achieve what I want?

09-27-2011, 05:23 PM
Unless I'm missing something just omit that first condition:

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

09-27-2011, 05:56 PM
Ah, hmm… that’s interesting. Why does this work?

Oh, I think I get it. It says “if HTTP_HOST is not example2.com redirect to http://example2.com”, right. Stupid me! That’s why originally it always used to redirect to domain 1 when domain 2 was added.

I’m gonna go now and bang my head against the wall.