Redirect Old Domain to New Domain via Apache .htaccess - arrow-sign

Redirect Old Domain to New Domain via Apache .htaccess

Planning before you redirect old domain to new domain

It’s important to do some planning before you do any link changes and we advise you to consult with an SEO professional before you do any of these just to be one the safe side.

Before you do anything take a deep breath and go make a backup.

You don’t want to lose search engine rankings and/or upset users that can’t find the pages they were looking for.

In this post we assume that you’ll be changing the top domain name only and keep the current link structure. This is what we had to do with the WPMove site. It was initially set up to run on .org version of the site but later we acquired the wpmove.net domain name and decided that it would be better. This looks like a simple change but do check Move WordPress Site to Another Domain Checklist to see that there are details that have to be considered.

SEO people generally advise to have an excel file that contains information about old links and where they are pointing to. I will use | as a separator but you can think of this as cells in the excel file.

For example:

  • /wordpress-migration | /services/wordpress-migration | 301
  • /woocommerce-migration | /services/woocommerce-migration | 301

Doing the redirection of all the pages from the old domain to the new domain will make sure that the SEO information is transferred to the new location and also visitors will see the correct pages and not land on a page not found error page. 

How to Redirect from the Old Domain to New Domain via Apache .htaccess

The following .htaccess rules apply to the Apache web server. Maybe openlitespeed server could also parse them but we haven’t tested that.

We used the following .htaccess rules and added them to the .htaccess file where the old domain was. We set up the new domain in a new folder and kept the old domain in their own folder to keep things as organized as possible. We have specifically specified the target location. 

We have used ## as a hint that that line is commented out on purpose.

# Redirect to example.com except files that start google, other search engines and certbot requests
# Blog post: https://wpmove.net/893
<IfModule mod_rewrite.c>
	##RewriteCond %{HTTPS} off
        ##RewriteCond %{HTTP_HOST} ^(www\.)?oldexample.com$ [NC]
	RewriteCond %{REQUEST_URI} !^/?(\.well-known|google|bing|yahoo)
	##RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,QSA,R=301]
	RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [L,QSA,R=301]
</IfModule>

Explanation of the .htaccess file and redirect rules

In our case we want to redirect ssl and non-ssl traffic to the new location. That’s this line.

##RewriteCond %{HTTPS} off

If your new domain is in the same folder as the old you may need to uncomment that line. Otherwise there would be a redirect loop. The new and the old site will redirect to itself.

[NC] flags mean that the search will be done in a case insensitive manner which means that the browser can send a request to OLDEXAMPLE.com and it would still match the rule.

##RewriteCond %{HTTP_HOST} ^(www\.)?oldexample.com$ [NC]

We have this line because we want to allow requests made to the .well-known folder which is required to renew any certificates issued by certbot (Let’s Encrypt) service.

Also there could be verification files from Google, bing etc. that need to stay in those services to check if you still own the domain name.

RewriteCond %{REQUEST_URI} !^/?(\.well-known|google|bing|yahoo)

The following line is where the magic happens. We will take the requested page and send it to the new location. The additional options in the square brackets are important.

RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [L,QSA,R=301]

L – means last rule. No other apache rewrite rules should happen after it

QSA – means that Query String Append => which means that the server will append any parameters that were sent to the old link will also be transferred to the new one.

May think that’s not important but it is. Think of UTM parameters. What if you have content or ads pointing to the old site that pass Google Analytics UTM parameters?

You want them transferred as well. e.g. /?utm_source=google&utm_medium=ppc

R=301 is what type of the redirect will be used. Since you’re moving domains 301 (permanent redirect) makes sense.

To read more about apache mod_rewrite flags check this page https://httpd.apache.org/docs/2.4/rewrite/flags.html

Image credit: Free Arrow Sign Stock Photo by Michal Zacharzewski on FreeImages

Do you have a recommendation how to improve the post or a question?

Leave a Comment

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