Apache webserver: redirect requests for domain.com to www.domain.com

Monday, March 22, 2010 posted by Till

Many webmasters want to redirect users that access their websites with "domain.tld" automatically to "www.domain.tld". If you use the Apache web server, you can do this by using Apache rewrite rules.

Add a .htaccess file with the following content in the root directory of the website:

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

If you use ISPConfig as hosting control panel, you can add these rules also in the Apache directives field of the website instead of a .htaccess file

Be Sociable, Share!



6 Responses to “Apache webserver: redirect requests for domain.com to www.domain.com”

  1. nicram says:

    It do not work for me, anyone can confirm if it;s working or not?

  2. RaZer0r says:

    did not work for me either… something must be off, looking for another sollution

  3. nicram says:

    RaZer0r: i think i find the way it is working.
    I made 2 sites in ISPConfig: 1 – without www (example.com), and 2 – with www (www.example.com). Then i setup redirection inside ISPConfig (without htaccess file, but it may also work) inside 1st site, and then, it started to work :) Try it :)

  4. rkeles says:

    Should be like this:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
    RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

  5. Till says:

    I’ve updated the rewrite rule.

  6. Gauthier says:

    Hello, this is still not working for me.

    May be because I am working with apache AND tomcat. This is how the apache directive looks like in ispconfig2:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
    RewriteRule (.*) http://www.mydomain.com/$1 [L,R=301]
    RewriteRule ^/servlet.gupld(.*)$ /tomcat_mydomain/servlet.gupld$1 [PT,QSA,L]
    JkMount /tomcat_mydomain/* worker1

    Tomcat rewrite rule IS working. Any help would be very apreciated.

    Thanks

Leave a Reply