Forum Moderators: open
It is a good practice to stick to 1 version so that you won't lose PR because Google treat both of them as different site (although it is usually the same!).
.htaccess is the best way to do it. Open a text editor (i.e. notepad) and copy paste the following code. Change 2 occurances of yourdomain to your real domain. (if not .com, change as well) Save the file as .htaccess. Then upload this file to your web root page (i.e /public_html/ ...)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.yourdomain\.com
RewriteRule ^(.*)$ [yourdomain.com...] [R=301,L]
I have FrontPage extensions installed and after I appended this code to my existing .htaccess, I won't be able to publish files anymore. So, I need another solution - using PHP.
Since my entire site are PHP scripts (*.php) and I always have a same header file included in the top of each file, I have added this code to my header file and it will redirect (301) any pages loaded from the non-www domain to the www domain.
if ($_SERVER['HTTP_HOST']!= "www.yourdomain.com")
{
if ($_SERVER['SCRIPT_NAME']!= "/index.php")
$url = "http://www.yourdomain.com" . $_SERVER['SCRIPT_NAME'];
else
$url = "http://www.yourdomain.com/";
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
}
If you want to redirect www to non-www instead, just remove the 'www.'s in the code.
You must use 'HTTP_HOST' instead of 'SERVER_NAME' because 'SERVER_NAME' would always be the same as it depends on your server configuration.
Ok, hope this is helpful. If GoogleGuy is reading this, it is good if you can confirm these codes will work nicely with Google. :)
And for those who know how to make the .htaccess works with frontpage extensions, please post solution here. Thanks.