When your website can be accessed via several URLs, such as http://www.example.com & http://example.com, you might want to decide a best URL and use only that URL whenever you link back to your site, because this helps your site in terms of SEO.
According to Matt Cutt’s blog:
Q: What is a canonical url? Do you have to use such a weird word, anyway?
A: Sorry that it’s a strange word; that’s what we call it around Google. Canonicalization is the process of picking the best url when there are several choices, and it usually refers to home pages.
Assume that a googlebot visited your website via http://example.com but you want your default URL to be http://www.example.com. First, what you need to do is, detect this situation, let the robots know which URL you want and set 301 (permanent) redirect to that URL.
One very important variable we will need in the codes is the $_SERVER which is a predefined variable. We will need $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] here.
$_SERVER['HTTP_HOST']
This variable returns the domain name of your website ONLY, without trailing slash and it includes www if your current address has it.If your current page is:
- http://example.com – it returns example.com
- http://example.com/ – it returns example.com
- http://www.example.com – it returns www.example.com
- http://www.example.com/ – it returns www.example.com
- http://example.com/wordpress/ – it returns example.com
- http://example.com/faq.php – it returns example.com
- http://example.com/?p=9 – it returns example.com
$_SERVER['REQUEST_URI']
This variable returns URI which was given in order to access the page, a slash is always included.If your current page is:
- http://example.com – it returns /
- http://example.com/ – it returns /
- http://www.example.com – it returns /
- http://example.com/wordpress/ – it returns /wordpress/
- http://example.com/wordpress/a-blog-post/ – it returns /wordpress/a-blog-post/
- http://example.com/faq.php – it returns /faq.php
- http://example.com/?p=9 – it returns /?p=9
Here I have 2 examples of codes, from www to non www and vice versa.
This is the codes you should use if you want to do a permanent redirection from http://example.com to http://www.example.com
<?php if (substr($_SERVER['HTTP_HOST'],0,4) != 'www.') { header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); } ?> |
It simply check if the first 4 characters are not “www.”, then it set a permanent redirection to the www version.
This is the codes you should use if you want to do a permanent redirection from http://www.example.com to http://example.com
<?php if (substr($_SERVER['HTTP_HOST'],0,4) == 'www.') { header('HTTP/1.1 301 Moved Permanently'); header('Location: http://'. substr($_SERVER['HTTP_HOST'], 4).$_SERVER['REQUEST_URI']); } ?> |
It simply check if the first 4 characters are “www.”, then it set a permanent redirection to the non-www version.

