Redirect Old Domain to New Domain via .htaccess

When you migrate a site from one domain to another, it is very important that you don’t break all the links that you built to your old domain. Proper redirection of all the pages on the old domain to the same location on the new domain will ensure that visitors to the old domain will end up in the right place.  A failure to redirect will result in a loss of visitors as well as search engine rankings.

We are assuming that your web server uses Apache for the purpose of this tutorial.  If you have not made any changes to your overall site structure, but have simply relocated the site in its current state, you can add the following lines to your .htaccess file located at the root of your old domain:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

If you have made changes to your site structure, you can still use the lines above on your old domain, but you will also need to create redirects in the .htaccess file on your new domain to handle the specific site changes.

To redirect a single page to a new location on the same domain, use the following syntax:

Redirect 301 /old/old.htm http://www.domain.com/new.htm

Facebook
WhatsApp
Twitter
LinkedIn
Pinterest

Leave a Comment

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

Related Articles from the Series

Common Image WpDeveloperTips
Important Notes for .htaccess
As a configuration file, .htaccess is very powerful. Even the slightest syntax error (like a missing...
Common Image WpDeveloperTips
Change Default Index Page
This rule tells the server to search for and serve “business.html” as the default directory index. This...
Common Image WpDeveloperTips
Commenting .htaccess Code
Comments are essential to maintaining control over any involved portion of code. Comments in .htaccess...
Common Image WpDeveloperTips
Htaccess Definition
Apache server software provides distributed (i.e., directory-level) configuration via Hypertext Access files....
Common Image WpDeveloperTips
Secure Directories by IP Address and/or Domain
In the following example, all IP addresses are allowed access except for 12.345.67.890 and domain.com:...
Scroll to Top