Scroll page back to the top

Enforce 'www.' on domain name with .htaccess

Redirecting domain.com to www.domain.com is pretty straight forward and worth knowing.

When you are running your website you should decide what version you want to use - either with 'www' or without - and you should stick to it whenever you link to your site.
You might soon find out that this consistency helps you with your SEO campaign, therefore it is pretty important element of any successful website.

To create the redirect to 'www' create the .htaccess file in the root of your site and open it for editing (or open the existing one).

Now start by checking whether the mod_rewrite is enabled on your server:

<IfModule mod_rewrite.c>

</IfModule>

Now, in between these two tags type the following, replacing domain name with the one you're working on:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain.\com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

Your entire block of code should now look like this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
</IfModule>

If you now open your browser and navigate to the site using the domain name without 'www' - you'll see that you will be automatically redirected to the 'www' version of your domain.

 
 
 
Add a comment
Add Comment