Using Mod_Rewrite To Make A SEO Friendly Site Redirect

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Updated: 23-Oct-2011

Im in the middle of moving my site from my old server under my desk to a colocated machine so this will be a short but relevant post.

Changing Domain Can Lose Traffic And Page Rank

Up till now I have used a reconditioned desktop sitting under my desk to host my site. This setup while convenient required me to use a dynamic dns service to route traffic to my home ip address.

As a result of this redirect anyone who has bookmarked my site will most likely have bookmarked the dynamic dns url as opposed to neiland.net. This means that if I simply turn off the redirect many people bookmarks will get broken and I might lose some traffic.

In addition to losing traffic, google's index of my site would now link to non existent pages and my page ranking would suffer. This means I would have to start nearly from scratch rebuilding my ranking.

Mod_Rewrite To Create a 301 Redirect

Fortunately it is possible to redirect traffic from the old server hosted site to the new one using a 301 redirect. A 301 redirect is a special response code a page sends to a browser to tell it that the page being visited has been permanently moved to a new location as specified. This causes the browser to seamlessly redirect to the new address.

In addition to this when google sees a 301 for a page it updates its index of the old page with the new url while maintaining the old pages rank scoring. This allows you to keep your old page ranking and google to update its index of the site with the new links.

A Note On Browser Bookmarks

While there is a specification that browsers should update bookmarks when they revisit a bookmark and encounter a 301, this behavior does not seem to take place in my experience. Hopefully in the future browsers will do this but at least in the mean time people visiting my site will be redirected to the correct url.

Using .htaccess To Redirect To New Server Domain Home Page

There are several scenarios we need to consider for redirecting. The first one is where we want to redirect all pages from an old website on an old server to the homepage of a new site on a new server. This is the easiest to accomplish. To do this use the following in your .htaccess file on your old website.

Options +FollowSymlinks
RewriteEngine on
#redirect all pages on this site to the home page of the new site using a 301 redirect
RewriteRule ^(.*)$ http://www.newdomain.com/ [r=301,nc]

Using .htaccess To Redirect To New Server Domain Maintaining Site Structure

The next scenario expand on the previous one by adding the condition that we want to redirect all pages on the old domain to their corresponding location on the domain on the new server. This is accomplished with a small change to the .htaccess file where we append on the original url string sans the original domain.

Note: This assumes that the site structure itself has not changed.

Options +FollowSymlinks
RewriteEngine on
#redirect all pages on this site to its corresponding location on the new site using a 301 redirect
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]

Using .htaccess To Redirect To New Domain On The Same Server Maintaining Site Structure

To further expand on this, we now add the common case where we are just changing the domain nothing else. This requires adding some intelligence so that when someone visits the old domain the redirect kicks in, but when they visit the new domain the htaccess file knows not to redirect.

To do this we add a conditional check before the redirect that only allows the redirect if either of the two conditions are matched. These conditions being that the domain being visited is the old one with or without the "www".

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$[OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]

Using .htaccess To Force Redirect To WWW Domain

We can make use or the last example for one last thing. If you want to always have the www in front of your domain or never have it you can use the following.

Force WWW with .htaccess

Options +FollowSymlinks
RewriteEngine on
#If domain does not include www
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Force No WWW with .htaccess

Options +FollowSymlinks
RewriteEngine on
#If domain includes www
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ http://domain.com/$1 [r=301,nc]

Hopefully this will help out the next time you have to change your domain.

Update: Force Redirect For Multiple Domain Names

I recently needed to make write a rule that all domains hosted by a single muracms instance would redirect to www.[domainname]. Obviously the above rule would not work so I came up with this.

#force www for multiple domain names
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Reader Comments

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing