301 Redirect Using .htaccess

A commonly overlooked factor in search engine optimization for websites is the “to www or not to www” question.  Singularly speaking, search engines have no preference for your site to contain the “www” prefix, however failure to specify can negatively impact your search engine rankings.

Do note that in order for the .htaccess file modification below to function properly, your webserver must allow mod_rewrite.

Consider the following scenario.

You submit your xml sitemap to google.  Your links all contain “http://www.yourwebsite.com”.  Great!  Google search engine results will display your website with the appropriate prefix.

But now consider that somerandomwebsite.com adds a link to your website from theirs.  And they omit the “www.”, leaving only “yourwebsite.com”.  Even though technically “yourwebsite.com” and “www.yourwebsite.com” are the same website, search engines see 2 websites.

Having two sites means, the ranking of that domain is divided (unequally; dependent on inbound and outbound links) into two. It is fine if the site is highly ranked, and users have no problem discovering the site. However, for sites which do not have very high ranking, it is perhaps more ideal to confine to using one particular domain name for accessing the content.


Fortunately, this doesn’t have to create problems for you, as canonical domain rules can easily be set using your .htaccess file, and google’s webmaster tools as a supplementary specifier.

Step 1

If you don’t already have your website set up through Google’s Webmaster tools, start here.  If you do, continue…

Under your website configuration in google’s webmaster tools, select “Site Configuration” -> “Settings”.  Under the section titled “Preferred Domain”, select the appropriate prefix for your URL and save your changes.  Google provides additional information on the webmaster tools help page.

Step 2

IF you have opted for the more traditional “www” prefix, and you are not using wordpress or another CMS which will automatically create the appropriate URL rules for your website, you’ll need to create a .htaccess file.

This should be a plain text file, which you may need to temporarily name without the “.” prefix so that it remains visible to you, and should contain the code below.  Be sure to replace the URLHERE tags with the URL of your website prior to uploading via FTP or through your web host’s file manager.

RewriteEngine OnRewriteCond %{HTTP_HOST} ^URLHERE.comRewriteRule (.*) http://www.URLHERE.com/$1 [R=301,L]

Once you’ve replaced the necessary URL, upload the edited .htaccess file, append the “.” so that the filename is “.htaccess”, and test it out!  On some servers, it may take up to 1 hour for the changes to take effect (GoDaddy for example tends to refresh .htaccess files and apache server settings only hourly).

Step 3

Once you have created and uploaded your .htaccess file, and it ‘appears’ your changes have been successful in redirecting all non-“www.” url’s to their “www.” version, check your header statuses to ensure that a correct 301 redirect has been detected.  You can accomplish this either by doing a quick google search for “header redirect checker”, or here.


.htaccess files can be hazardous if misused, or improperly coded, and as mentioned in the beginning of this post- some web hosts may take a bit to refresh servers, showing changes.  Don’t freak out yet! Go smoke a cigarette, go for a walk, do your dishes, come back after an hour or so.  My first experience modifying/using htaccess was a bad one- apparently because I was unaware that hosted servers often take a bit to detect/refresh.

Related Posts

2 Comments + Add Comment

  • Wow! Thank you! I constantly needed to write on my blog something like that. Can I implement a fragment of your post to my website?

  • Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but other than that, this is great blog. A great read. I’ll definitely be back.

Leave a comment