J. Cornelius

Always add WWW to your URLs

February 19, 2004

It's always a good idea to have all the traffic for your site go to www.yourdomain.com. Through the magic of Apache mod_rewrite, you can keep your URLs clean and tidy.

Often times people will just type your domain name (i.e. "yourdomain.com") into their browser to get to your website. This can cause some interesting and undesired issues, especially if you plan to do any detailed log analysis and statistics reporting.

It's also a bad idea from a SEO point of view because search spiders can see "yourdomain.com" and "www.yourdomain.com" as two seperate sites with the same content. Why this is bad is for another discussion but trust me, it's bad.

If your server is running Apache and you have access to change the servers configuration file you can use the code below to force the server to add 'www.' to the front of urls that don't have it.

RewriteEngine On RewriteCond %{HTTP_HOST} !^www.domain.com RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L]

This should be placed inside the VirtualHost container for your website.

You can also use this in various forms to force certain requests to different URLs. See the Apache URL Rewriting Guide for more info.

Was it good for you?

Post to Digg Post to del.icio.us Post to ma.gnolia Post to Furl Post to Mixx

Tags

administration, apache, development, seo, standards

About

J Cornelius is a father, software developer, Web developer, and Formula 1 fan in Atlanta GA. He has a strange affinity for odd numbers, european sports cars, thoughtful analogies, and is hopelessly addicted to chips & salsa. Read more

Recent Articles