Richard Parnaby-King

Web Developer – PHP, Zend Framework and Actionscript 3

Posted on | | 1 Comment

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.

Posted By:

Comments

  • excellent. all sites be better with www. othwerwise people not know it on internet. it could be shop or phone call.

    always put www. on website to show it website!

  • ABOUT

    Having eight years of programming experience in PHP, Zend Framework and ActionScript3, I have a very strong working knowledge of object orientated programming.

    I am a PC Gamer! Playing FPS, RTS, RPG and the occasional MMO since 1996 I have a huge number of a variety of fast-paced games.

  • Recent Posts

  • Categories

  • RSS SUBSCRIBE TO OUR FEED

  •