Redirect domains without www (e.g. domain.com) to www.domain.com with apache rewrite rules

If all your visitors shall access your website with a URL like www.domain.com and not without www, use the following apache rewrite rule for redirecting them.

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

The apache rewrite rule can be added in a .htaccess file in the website root directory or if you use ISPConfig 2 or 3, you can also add the rwrite rule into the apache directives field of the website.

facebooktwittergoogle_plusredditlinkedinmail

One thought on “Redirect domains without www (e.g. domain.com) to www.domain.com with apache rewrite rules”

  1. Alan says:

    I think there should be a forward slash after {HTTP_HOST} like so:
    RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R]

    Cheers,
    Alan

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>