301 Redirect Your Domain w/ Apache Mod Rewrite Keeping File Path Intact

3 02 2009
651.gif I’m posting this because I Googled it to death this past weekend trying to find the code for it, and it was nowhere to be found. I’m positive I’ve used it before in the past too, but the code was not in any of my old projects either. After hours of googling for what I knew was a simple mod rewrite for .htaccess, I submitted to defeat and asked (more like begged) an expert on the subject. Here was the problem:

301 Redirect every URL from www.domain1.com to the coresponding URL (keeping the trailing file path intact) on www.domain2.com, when both domains are being pulled from the same directory on the same server, and respecting URL canonicalization issues (www vs. non-www).

This should be a readily available mod rewrite written about on SEO blogs worldwide right? Wrong.

I offered to pay my expert for the consult, but he refused, probably laughing at the trivial request I submitted. So here it is – hopefully good ole Google will pick up this post so the next guy coming along will find this, no problem:
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^www\.domain2\.com$
RewriteRule (.*) http://www.domain2.com/$1 [R=301,L]

Cut, paste into .htaccess file, use notepad, must have Apache server with mod rewrite enabled, works like a charm :) It worked the first time in fact, not like when I do a mod_rewrite and have to test it out 20 times to get it right!

Actions

Informations

6 responses to “301 Redirect Your Domain w/ Apache Mod Rewrite Keeping File Path Intact”

4 06 2009
ojpt (18:08:32) :

this is helpful! I wonder if this is enough to insure that only domain2 show up in search results.

4 06 2009
Chris H. (19:08:45) :

Yes, that’s exactly what it does. After apx. 30 days, only domain 2 will show up in the search results, and best of all, PageRank is passed from the old URL’s to the new ones.

4 01 2011
Richard Cummings (08:30:45) :

This is just the information I was looking for. Mod/Rewrite is certainly not an intuitive file to modify. Anyway, thanks a bundle for the info. I have bookmarked this page for whenever I need to do this, Richard

26 06 2012
Doug (18:09:26) :

Strange. I was just about to do this, but then noticed after I simply had domain1 point to the same directory as domain2 that’s all that was needed. The URLs still worked.

Is that sufficient do you think?

doug

12 08 2012
Chris H. (08:18:13) :

No Doug, the problem with doing that is you have 2 duplicate websites showing up under the same domain name. Google will decide which one it wants to rank you for, and then there is also the possibility that people will link to the wrong one.

24 10 2012
Steve Dowe (06:29:59) :

Just the quick tip I was looking for to redirect my blog.

Thanks!

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>