Richard Parnaby-King

Redirect website from non www to www

Canonicalisation issues occur when a website is accessible from two different addresses, for example ‘www.your-site.com’ and ‘your-site.com’. The worst that can happen is that your website gets indexed twice, one version gets seen as duplicate content of the other, and your website does not rank as high as it possibly could. On an apache server the fix is fairly simple:

[c]
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.your-site.co.uk$
RewriteRule ^(.*)$ https://www.your-site.co.uk/$1 [R=301,L]
[/c]

Line 3 is a condition. The redirect on line 4 will not be processed unless the host is NOT www.your-site.co.uk. What this means is if you have multiple domains all pointing to the one host/website, instead of having one line per domain (see example below) it is easier to do a not condition.
[c]
RewriteCond %{HTTP_HOST} ^your-site.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.your-site.com$ [OR]
RewriteCond %{HTTP_HOST} ^your-site.com$ [OR]
RewriteCond %{HTTP_HOST} ^my-site.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.my-site.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.my-site.com$ [OR]
#etc
RewriteRule ^(.*)$ https://www.your-site.co.uk/$1 [R=301,L]
[/c]

The last line does the actual redirect – it will take whatever page you are trying to access and tell your browser to do a 301 redirect to the www version of your website and access the page there.

This post has shown how to do a 301 redirect from a non-www version of a website to the www version of the website using an .htaccess file on an apache server.