Redirection

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.

8 Responses to Redirection

  1. Maniquí says:

    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.

  2. Nicholas says:

    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!

  3. joseph says:

    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.

  4. HB says:

    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!

  5. RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www [NC]
    RewriteRule ^(.*)$ http://no-www.org [R=301,L]

    Though I’ll usually redirect requests for the pointless www cname to a re-education document, cookie them, show it only once (or twice), and silently redirect them to the correct domain

    :]

    Please update your ‘history’ to include the oft used, but now similarly deprecated, “web.domain.tld”

  6. grmbl says:

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

  7. TheCosmonaut says:

    I’m running into a strange thing with this: the code above works fine in all instances EXCEPT if I click a non-www link in Gmail… any ideas what might be causing that?

  8. I have used this and it’s great.
    I have used no-www.org .htaccess file and it’s great too!

Leave a Reply

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Bad Behavior has blocked 222 access attempts in the last 7 days.