7 Useful htaccess Redirects For Migrating A Website To A New Domain

7 Useful HTACCESS Redirect Rules

I always find myself looking up the same things over and over again when dealing with htaccess redirects and migrating sites to a new domain. This blog post is long overdue so I can finally have a quick reference for the main types of htaccess redirect rules that I use. 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. 404 error pages = SEO disaster.

Redirect 1: Redirect A Single Page To A Page On The New Domain

Redirect 301 /page.html http://newdomain.com/page.html

Redirect 2: Redirect Root Domain To New Domain Root

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

Redirect 3: Redirect All Pages To Single Page On New Domain

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

Redirect 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]

Redirect 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

Redirect 6: Force WWW To Be Used

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

Or Force NON-WWW To Be Used

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

Redirect 7: Force Use Of HTTPS

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

Or for a specific folder (say your website store page)…

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

BONUS: Redirect With Or Without Trailing Slash

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


Mike Doubintchik

Author Mike Doubintchik

More posts by Mike Doubintchik

Leave a Reply