To make all the traffic for http://example.com/ politely and silently redirect to http://www.example.com/ simply create a file called .htaccess (if it doesn’t already exist) and add the following to it, changing the red text to match your domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
These directions are for Apache and require that mod_rewrite be enabled. If you have any idea how to do this for IIS or any other web server, let me know and I will place the information here.
Hi!
thanks for this tip!
I was having some problems wit the Dean Edward’s IE7 script when visitors didnt access thru the “www” url.
So I set thos rules for rewriting the URL and voila! problem fixed…
Thanks again.
Comment by Maniquà — February 16, 2006 @ 6:15 pm
I tried putting the data you have above into an .htaccess file, but it didn’t do anything that I could see. Are there any specific AllowOverride options that you have to specify in the configuration file for it to work?
Thanks!
Comment by Nicholas — March 22, 2006 @ 6:44 pm
I’m a die-hard anti-www guy, but I had to use your version for the site I’ve linked to, for some vague flash-related reason. The non-www version just wouldn’t work…
Thanks.
Comment by joseph — April 21, 2008 @ 6:08 am
I’m making a site that will definitely benefit from an identifier for the “main” site, since people can sign up for their-user-name.mydomain.com, and your script works like a charm when it comes to disambiguation. Thanks!
Comment by HB — June 12, 2008 @ 1:10 am