Over the last year I found myself migrating several websites to new domains. Therefore, I have decided to write a post about the 5 useful htaccess tricks for website migration. Htaccess redirects are important because you don’t want Google to find a bunch of 404 error pages when you shut down your old domain.

Firstly, I will put it like this


“404 error pages = SEO disaster.”


5 useful htaccess tricks for website migration


1# Force domain to use WWW

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301,NC]

Or Force domain to use NON-WWW

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301,NC]

2# Force domain to  use secure protocol of HTTPS

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

 or force specific folder to use of HTPPS (like store front page)

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder 
RewriteRule ^(.*)$ https://domain.com/folder/$1 [R,L]

3# Redirect Root Domain To New Domain Root

RewriteEngine On
RewriteRule ^$ http://newdomain.com/ [R=301,L]


4# Exclude Certain Pages From Redirect 3 (above)

RewriteEngine On
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !^/page1.html$
RewriteCond %{REQUEST_URI} !^/folder/subpage1.html$
RewriteRule .* http://new-domain.com/page/ [R=301,L]

5# Redirect All Pages To Their Same URL On New Domain

RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old-domain.com [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [L,R=301,NC


Extra tip to redirect  with or without Trailing Slash

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^page-to-redirect/?$ http://new-domain.com/page-redirect/ [NC,R=301,L]

Follow me on Instagram