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.

11 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!

  9. Date says:

    Thanks for the text! I used it on my website since it the http://www. and the http:// was seen as differents sites, so I choose the www :) and worls fine now.

  10. Marco says:

    Hello.
    Could you tell me what should I do to config this on IIS?
    Thanks.

  11. Thomas B. D. says:

    From what I’ve experienced, the effect of using http://www.domain.tld is that users tend to prepend www. to ANY and ALL subdomain URL’s you’ll send them.

    E.g. “Just go to mail.mydomain.com…” user does this: “www.mail.mydomain.com” which, first of all, doesn’t work and, second, makes no sense.

    I prefer without ‘www’ since it saves typing and ‘www’ has nothing to do with my website.

    Imagine these examples:

    mydomain.tld
    img.mydomain.tld
    mail.mydomain.tld
    cdn.mydomain.tld

    In order of appearance:

    Website
    Image host
    Webmail
    Content Delivery Network host.

    Makes sense in my head, and ‘www.’ wouldn’t make any sense here.

Leave a Reply

Your email address will not be published. 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>