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

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

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

  1. Avatar

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

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

  3. 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. Avatar

    I’ve updated the rewrite rule.

  5. Avatar

    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.


  6. Avatar

    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.


Leave a Comment