Forcing the use of www domain prefix

Browse: Tutorials » .htaccess
This guide will show you the required steps to force users to browse your site using the www. prefix rather than just the domain name.

Example: www.gilly.org.uk not gilly.org.uk

This is achieved by using the htaccess file and checking the URL being used and if it does not match your prefixed domain name it silently redirects the user to the correct domain.

The following 3 lines are required to achieve this.

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.gilly\.org\.uk [NC]
RewriteRule (.*) http://www.gilly.org.uk/$1 [R=301,L]

You need to modify the following line to correspond to your domain name, it should be your full domain name with the www. prefix, as the code checks all URL's against this and if they do not match it redirects them.
It is also necessary to put slash's in front of the '.' except for the one following the www.

RewriteCond %{HTTP_HOST} !^www.gilly\.org\.uk [NC]

The following line redirects the users to your full domain name with the www. prefix.

RewriteRule (.*) http://www.gilly.org.uk/$1 [R=301,L]

Again this line needs to be changed to point to your domain so that users are redirected correctly, the $1 at the end of the line is required so that if the user is requesting a file or directory in there request they are directed there rather than back to your homepage.

I hope this is useful to someone as I know I had problems when i first came to try and figure this out.