Article written

  • on 05.12.2007
  • at 10:27 AM
  • by TYPELiFE

TIP: Force "www" in URL with .htaccess 2

Dec5

This neat little trick will force all browsers that are trying to access your website from domain.com into a standardized gateway of www.domain.com for increased search engine consistency, as well as a consistent aesthetic appearance to regular users.

Add the following snippet of code to the .htaccess of your public html directory where you would like to use this.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.your-website\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Updated .htaccess to allow subdomains at the suggestion of Jim, The Blogaholic. If you don’t want this functionality, just remove the third line!

Voila, 301 forwarded to the same homepage every time!

(BTW, this also prevents having such a varying pagerank between domain.com and www.domain.com some of the time.)

subscribe to comments RSS

There are 2 comments for this post

  1. Jim says:

    Don't forget to exclude sub-domains when re-directing to www (otherwise you won't be able to have sub-domains for your website!)
    http://www.theblogaholic.com/2011/01/16/force-www…

Please, feel free to post your own comment

* these are required fields

Internet Marketing Strategy is powered by WordPress and FREEmium Theme.
developed by Dariusz Siedlecki and brought to you by FreebiesDock.com