SEO Tips – Mod Rewrite Non-WWW to WWW
Do you know that http://www.TIMMGuru.com and http://TIMMGuru.com are distinct URLs and treated different by search engines. Google has always treated them as different distinct URLs until recently – when they upgraded to Big Daddy and resolve the canonical issue.
Still I think it would make life easier if we just stick to one URL for our domain. That is, if you have elected the www version, even if your visitor use the non-www version, your web server will present the www URL.
How Do You Re-direct Non-WWW to WWW?
If you are using Apache web server (Linux), you can use mod rewrite to re-direct non-www to www. You will need to have access to the web server – via FTP or CPanel File Manager.
The file to be edited is .htaccess
Add the following lines to the .htaccess file:
RewriteEngine On RewriteCond %{HTTP_HOST} !^www..* RewriteCond %{HTTP_HOST} !^$ RewriteCond %{HTTP_HOST} ^([^.]*).(com|com.sg) RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=301,L]
The above script will redirect:
http://yourdomain.com or http://yourdomain.com.sg
to
http://www.yourdomain.com and http://www.yourdomain.com.sg
If you are not using the Singapore dot com domain name but Malaysia dot com or Indonesia dot com, then replace the “com\.sg” with “com\.my” or “co\.id”.
One advantage of the above script over the one that I adopted from Search Engine News is that this script will leave subdomain.yourdomain.com intact and not re-direct it to www.subdomain.yourdomain.com or www.yourdomain.com/subdomain/.
Redirecting WWW to Non-WWW
And here’s a simple script for you to add into the .htaccess file if you want to re-direct www to non-www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mysite.com [nc]
RewriteRule (.*) http://mysite.com/$1 [R=301,L]
The above script will redirect:
http://www.mysite.com
to
http://mysite.com
Even if you have sub-domains, the above script will work beautifully.
The above scripts had been tested and used in many of domains I owned and they are working perfectly. Here’s the demo:
May 4th, 2007 at 3:04 pm
[...] example for this is an entry by a Shi Hengcheong, on Mod Rewriting Non-WWW to WWW domains, which solves the canonical domain [...]