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!

Related Posts



Actions

Informations

2 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.

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>