Forum Moderators: open

Message Too Old, No Replies

non-www domain to www conversion using 301 redirect

using .htaccess or PHP

         

AthlonInside

8:24 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My site is indexed by google in 2 versions - www.mydomain.com and mydomain.com.

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]

** Learn from jdMorgan.

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.

jranes

9:58 pm on May 12, 2003 (gmt 0)

10+ Year Member



I just implemented this on one of my sites. The non www version had the high pr but I went with www because it is the standard. It has been a while now and I wonder how long till the pr gets recalced. SJ still has the bad pr for www, I wonder if anyone else has crossed this bridge, how long did it take to get the pr redone?

AthlonInside

4:36 pm on May 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is, do 301 really pass the PR?!