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

Facebooktwittergoogle_plusredditlinkedinmail
Tagged on:         

7 thoughts on “Apache webserver: redirect requests for domain.com to www.domain.com

  • August 15, 2010 at 2:43 am
    Permalink

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

    Reply
  • March 18, 2011 at 2:08 pm
    Permalink

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

    Reply
  • March 21, 2011 at 11:45 am
    Permalink

    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 🙂

    Reply
  • August 1, 2011 at 1:01 pm
    Permalink

    I’ve updated the rewrite rule.

    Reply
  • May 14, 2013 at 1:12 pm
    Permalink

    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

    Reply
  • September 23, 2015 at 7:01 pm
    Permalink

    I guess this article is outdated for ISPconfig 3 as this can be done within one of the configuration pages and there is no need for manual changes in the apache config file.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *