How-To

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

Changing 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} ^olddomain.com$ [NC] RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]

</IfModule>

The code above is a 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} ^olddomain.com$ [NC,OR] RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC] RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]

</IfModule>

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 www.groovypost.com/forum/ to forum.groovypost.com.

<IfModule mod_rewrite.c>

RewriteCond %{HTTP_HOST} ^forum.olddomain.com$
RewriteRule (.*)$ https://www.newdomain.com/forum/$1 [R=301,L]

</IfModule>

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 https://www.newdomain.com/newpage.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.

8 Comments

8 Comments

  1. solaris

    August 30, 2016 at 5:44 pm

    Hi Brian,

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

    • Steve Krause

      August 30, 2016 at 6:56 pm

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

  2. solaris

    October 24, 2016 at 2:36 am

    thx, you are welcome

  3. Benny

    January 6, 2017 at 7:19 pm

    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} ^olddomain.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC]
    RewriteRule (.*)$ http://newdomain.com/$1 [R=301,L]

    Redirect 301 /oldpage1.html http://www.newdomain.com/newpage1.html
    Redirect 301 /oldpage2.html http://www.newdomain.com/newpage2.html
    Redirect 301 /oldpage3.html http://www.newdomain.com/newpage3.html

    • Steve Krause

      January 8, 2017 at 8:48 am

      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!

      -S

  4. Bala

    February 16, 2017 at 8:31 pm

    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

    • Steve Krause

      February 17, 2017 at 10:18 pm

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

  5. Zeljka Burazin

    January 31, 2018 at 8:43 am

    This redirect is not working, and instead I get a privacy error?

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^francishome.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.francishome.com$ [NC]
    RewriteRule (.*)$ https://www.francishome.ca/$1 [R=301,L]

Leave a Reply

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

 

To Top