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

facebooktwittergoogle_plusredditlinkedinmail

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

  1. 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 :)

  2. 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

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=""> <s> <strike> <strong>