How to Redirect an Old Domain to New Domain via htaccess 301

Migrating a Website to a new domain name is possible and fairly safe from an SEO standpoint as long as you setup the correct 301 redirects in your .htaccess file. This tutorial will explain the details for getting this done right.

apacheChanging domain names can be scary and unfortunately, it’s quite common. When migrating to a new domain name, not only do you risk breaking all your pages and links, but Google can also get cranky if all your indexed URLs end with a 404 – Page Not Found. Not only is this a terrible experience for your users, but it’s also a sure way to lose all your SEO juice aka Google Ranking. The good news is, if you’re running on an Apache website (which most of the internet is), there’s a simple solution.

Using .htaccess to Permanently 301 Redirect Old Domain to New Domain

If you haven’t made any changes to your site’s structure but are just transferring it, using the following code in your .htaccess file at the root of your old domain will not only redirect your users, it will also update Google’s index and pass along all your SEO Ranking.

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$ [NC] RewriteRule (.*)$$1 [R=301,L]


The code above is simple regular expression so you can do all kinds of nifty things with it. For example, if you want to redirect subdomains in addition to the root domain, you would use this code:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$ [NC,OR] RewriteCond %{HTTP_HOST} ^$ [NC] RewriteRule (.*)$$1 [R=301,L]


You can also do things like redirect an old subdomain to a directory on the new domain (or vice versa). I did this when I redirected to

<IfModule mod_rewrite.c>

RewriteCond %{HTTP_HOST} ^$
RewriteRule (.*)$$1 [R=301,L]


This last example shows you to perform one-off redirects for individual pages. This could be handy if you kept the overall site structure the same (using one of the redirects above) however you want to change a few URLs to the new domain. Just use the following:

Redirect 301 /old/oldpage.html

Being regular expression code, the possibilities are endless. A key takeaway, however, be extremely careful when making these changes to your .htaccess file. Messing with this file can break your Apache webserver and take down your site. So test, test and test again before making any changes. Need help? Drop a comment here or on our free support forum.



  1. solaris  

    Hi Brian,

    isn’t your first example code the same as the second one?

    • Yes! You are correct. Article updated. I probably made the goof editing the piece. Thnx!

  2. solaris  

    thx, you are welcome

  3. Benny  

    So if I want to make the sure the old root domain to new root domain, but I also don’t want to lose link juices on 10 of the other pages…would I combine the two method?

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^$ [NC]
    RewriteRule (.*)$$1 [R=301,L]

    Redirect 301 /oldpage1.html
    Redirect 301 /oldpage2.html
    Redirect 301 /oldpage3.html

    • Exactly right Benny!

      In fact, over time you will likely add additional REDIRECT 301 code in your .htaccess files throughout the life of your domain. Just keep adding them as you’ve done above.

      On my site here at groovyPost, I have several hundred from where I moved pages to new URL’s etc…

      Let me know if you need more help!


  4. Bala  

    Hi, We have 22 year old domain and 1 year old subdomain with new technologies. so we need to re-direct the old domain to new sub domain. What are all the key things nneds to cover for SEO factors? THANKS

    • That should be pretty straight forward. Just make sure you 301 redirect all the old links to the new subdomain URL’s and you should be fine. I would also recommend you flip your new subdomain to HTTPS also before you do the flip. HTTPS is very important now days from an SEO standpoint and if you’re going to take the time to redirect your old domain, you might as well flip it to HTTPS in the process so it’s one redirect instead of two (in the long-run).

Leave a Reply

Your email address will not be published. Required fields are marked *


To Top