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.

  4. This was really helpful! Thanks for posting it. I tried a bunch of other solutions I found on Google and this was the only one that actually worked!

  5. Thanks a lot worked a treat 🙂

    • rasul
    • May 7th, 2011

    Hi

    Thanks a lot
    That’s very very Helpful for me
    Good luck

    • Marc
    • May 21st, 2012

    I am using an in-house server for development work, thus the URL will be ‘localhost/website’ for clients to view the site the URL will not be the same, but something like this: xxx.dyndns.com/website. This works well except with WordPress which needs the exact URL. I have set up WAMP to read a .htaccess file, but cannot seem to get the correct syntax to pass the dysdns through to the locahost URL, any ideas?

    • philip lima
    • July 26th, 2012

    at least in 10.7.4 which i am using, i needed to go to the bottom left corner of the main window. with my iPhone connected, i selected from the drop down menu “Connecting tho iPhone opens” Then selected “No application”

    • Amy
    • September 10th, 2012

    Thank you for this..I think this is what I need to do but my question is do you still need to have hosting/wordpress set up on your old domain for this to work?

      • admin
      • September 11th, 2012

      You do need to have hosting set up at the old domain but all you need is an .htaccess file on the server. You don’t need WordPress or anything else.

  6. Thanks a million. this is quick and easy. I have a website with lots of files and was pretty concern

  7. The above code didn’t work for me, but this one did:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} olddomain.com$
    RewriteRule (.*)$ http://newdomain.com/$1 [R=301,L]

    Scott

  8. Well, posting took out some of the code.

  1. No trackbacks yet.