Change Your Domain Name and Keep Your Incoming Links With .htaccess And mod_rewrite

When moving our site from ablogapart.org to michaelkrol.com, this handy little bit helped move our entire website with four lines of code:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)ablogapart.org [NC]
RewriteRule ^(.*)$ http://michaelkrol.com/$1 [R=301,L]

This reference was extremely helpful.

This RewriteRule lives in the .htaccess file at ablogapart.org and tells any request coming in to that domain to swap ablogapart.org out and replace it with michaelkrol.com. This includes ANYTHING after the trailing slash, like a direct link to a previous post. So http://ablogapart.org/this/direct/link gets sent properly to http://michaelkrol.com/this/direct/link

The important part is the [R=301] which sends a 301 (Permanent) Redirect header. That tells search engines that the page has moved permanently.

Just imagine what you’d have to go through setting up individual forwarding links…

    • David J Rockafellow
    • August 20th, 2009

    In your example you use:

    RewriteCond %{HTTP_HOST} ^(.*)ablogapart.org [NC]

    Can this be written a manner that would be completely wild card? Say, for example you have a shared hosting account with poor DNS and other management and often strange IP’s and domains point to your site and you want to take advantage of that traffic but your too lazy to put an entry in your .htaccess for every domain or IP or just don’t know what all could point to you in the future?

  1. I’m new for writing the rules for .htaccess and i’m facing a big issue with this. the thing is that i’m having a in http://www.domainname.com/folder and i redireted to this with the subdomain of other domain like example.otherdomainname.com
    when i use subdomain to access the folder i can see the actual user path of the links.please any one can post the rewrite rule for this issue….ASAP.

  2. how do i move a blog thats on a sub domains to a folder on the main site would

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

    work?

  3. @mark rushworth Mark, yes that should work. Did you try it? The rewrite rule above accepts any subdomain, be it www or foo.

  1. No trackbacks yet.