Redirect an Old Domain to a New Domain with .htaccess

If you have decided to change your site’s domain and are hosting your old site on an Apache server, then redirecting traffic to the new site is relatively simple. The most important aspect of the move is to ensure that you don’t lose visitors and that you don’t lose any existing rankings with the search engines. A 301 redirect, or permanent redirect, will ensure that both human visitors and search engines alike are automatically and seamlessly pointed to the new domain.

Below is the code that you should place in your .htaccess file on the old site:

Obviously, you will need to replace the instances of olddomain and newdomain with your actual domain names and if your site isn’t a .com site, then that will need to change as well.

One thing to keep in mind when moving to a new domain is that the rule above will take a visitor to the equivalent URL on the new domain. In other words, if a user goes to http://www.olddomain.com/articles/about-me.html, then they will be redirected to http://www.newdomain.com/articles/about-me.html. But what happens if you actually moved that page to http://www.newdomain.com/about-me.html when you made the move to the new domain? Then you will have to add additional redirects to your .htaccess file to ensure that visitors are directed to the appropriate pages for these altered locations. Because of this, it is highly recommended that you don’t try to restructure your site at the same time that you make the move from one domain to another. One reason that this is a bad idea is because if something goes horribly wrong with your SEO, you won’t know whether it was because of the domain change or because of the site restructure.

Oh, and please be aware that your browser caches 301 redirects, so it is best to start with a 302 redirect when testing.

Comments

    1. If you use the code as-is in this article, you would NOT be able to access the admin of the old site. However, it is possible to add an exception to the redirect rule listed here by adding this line after the RewriteEngine On directive:

      RewriteCond %{REQUEST_URI} !^/?wp-.*

      This will ensure that anything starting with /wp- after the domain will not be redirected. This includes anything in the wp-admin directory as well as wp-login.php so you should be able to access the WordPress admin and login page. Just be aware that you may need to clear your browser cache since 301 redirects are cached.

  1. HI,

    If i have moved my domain to a new domain but the old home page is not the homepage on the newsite how can i redirect everyone to the newsite but those that are coming from that old site need to land on the old homepage on the new site.

    The remainder of the structure is as it was.

    Basically all i have done is move the old site to a new domain and created a new homepage on the new domain.

    Just want anyone being redirected from the old homepage to land on the old homepage on the new site.

    Hope that makes sense.

    example:

    Old site was on http://www.mywebsite.com

    New site is:
    http://www.mynewdomain.com/oldhomepage/

    Many Thanks

  2. Hi! I need to redirect an olddomain to new domain > oldsite.it to newsite.com

    In the olddomain there is a wordpress site, also in the newdomain (I’m already working on it and i made a new structure)

    I want to migrate the olddomain to the newhost where is also the newdomain

    When I will have the olddomain in my host I will need to create an htaccess file where I will insert this code:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
    RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
    in the htaccess of olddomain,

    right???

    since some links have changed for example:

    olddomain.com/about-me > newsite.com/about

    How can I do to redirect this type of links?

    i wait you thanks

    1. Correct, you can use the code in this post to redirect everything assuming the URL structure is the same. If some pages have changed, you will probably want to create one-off redirects for those (see https://wpscholar.com/blog/simple-redirects-with-htaccess/). Keep in mind that you want your more specific rules first (see https://wpscholar.com/blog/web-redirects-law-of-specificity/). So any one-off rules should go before the code from this post.

  3. Hi, This only Redirect the homepage only to the new domain, The old posts url are still working
    So how can i redirect everything

    1. This will redirect everything from the old domain to the new domain, not just the homepage. All paths off of the old domain will be pointed to the same location on the new domain. Placing the rules in the correct order can impact how redirects are handled on your site (see https://wpscholar.com/blog/web-redirects-law-of-specificity/). Keep in mind that the L in the brackets on the last line means that it is the last rule that should be applied. If you have other rules before this that have that L present, it could be why this rule isn’t being hit. You may also have additional .htaccess files deeper in your site structure that are impacting the effect of this rule.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.