Welcome to WebmasterWorld Guest from 54.144.3.134

Forum Moderators: Robert Charlton & aakk9999 & andy langton & goodroi

Message Too Old, No Replies

php header 301 and Google

     
6:56 pm on Jul 7, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 13, 2005
posts:80
votes: 0


Our backwards hosting company does not support the mod_rewrite module. So we will need to use a php header function to do a 301 redirect from our www version to our non-www version. The suggested code looks like this:
<?php
if (ereg("www", $_SERVER['HTTP_HOST']))
header("HTTP/1.1 301");
header("Location: http://example.com");
?>

I would like to know how Google friendly this is or would it be better to change our hosting company.

Also does the code look correct?

8:37 pm on July 8, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 15, 2005
posts:380
votes: 0


I used this with a result of successful merging www and no-www versions after a few weeks. I've written:

header("HTTP/1.0 301 Moved Permanently");
header("Location: http://www.example.com/");

This is almost exactly like your code, but I added 'Moved Permanently' and / at the end of domain name, but I'm not sure if this is necessary. I checked HTTP headers returned by server with Mozilla Live Headers plugin to ensure Googlebot receives proper HTTP Redirect headers.

10:21 am on July 9, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 30, 2002
posts:404
votes: 0


As long as you return the correct headers it should be exactly the same as doing it on the server
There are lots of examples of this in the php forum here [webmasterworld.com...]
12:05 pm on July 9, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 15, 2004
posts:89
votes: 0


"Our backwards hosting company does not support the mod_rewrite module. So we will need to use a php header function to do a 301 redirect from our www version to our non-www version."

Please excuse my "green-ness"...Are you 301 redirecting www.widgets.com to just widgets.com? For whatever reason, I thought it was supposed to be the other way around... Currently I have my widgets.com being 301 redirected to www.widgets.com . If someone visits widgets.com they are immediately pushed to www.widgets.com , and the header for widgets.com returned a "permanently moved to www.widgets.com" reply... I hope I've not gone about this backwards, heheheh.

Sincerely green,
Dave

12:06 pm on July 9, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:July 18, 2002
posts:154
votes: 0


This code should redirect a request of http://www.example.com/anyFile?anyQueryString to http://example.com/anyFile?anyQueryString

...
$queryString = getenv("QUERY_STRING");
$location = "http://example.com$PHP_SELF";
if ($queryString) $location ."?$queryString";
...
header("Location: $location");
...
exit;

added: exit; terminates script execution, you must not output anything before or after the header statements.

12:55 pm on July 9, 2005 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 31, 2003
posts:9068
votes: 4


Try this if you are using a global header file: it rewrites everything which isn't under www.example.com to the appropriate page under the www version:

<?php if ($_SERVER['HTTP_HOST']!= "www.example.com") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com".$_SERVER['REQUEST_URI']);
exit; }
else {
// add your usual headers here
};

It's a functional equivalent of the mod_rewrite redirect - although not all files will be rewritten, only PHP ones (eg. not direct requests for images or static files).

The disadvantage as I see it of your example code is that it only checks for the presence of the www, so if you park other domains pointing to the same space they would not get redirected to the primary domain name.

6:20 pm on July 9, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 15, 2005
posts:380
votes: 0


Are you 301 redirecting www.widgets.com to just widgets.com? For whatever reason, I thought it was supposed to be the other way around...

Even GoogleGuy himself said it doesn't matter which one we choose, it's just up to choose one and redirect to it. So it's up to us. I have several sites using www as cannonical, and a few with no-www, I don't see a difference in the results.

However, there are some directories that force www version and whois info links to your site with www version, so maybe for these reasons www versions would be better, but from Google point it's just to have one version, not both.

if ($_SERVER['HTTP_HOST']!= "www.example.com")

That's the way I do it, however I define $domain='www.example.com' to avoid writing the same string twice. Using this condition instead of preg_match('/^www\./', $_SERVER['HTTP_HOST']) has an advantage, that if a site have more domains parked (for example, the company wanted to reserve more domains from competition or bought more domains with different spelling), this redirect also merges possible duplicates from different domains.

9:43 am on July 10, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 13, 2005
posts:80
votes: 0


Thanks everybody for the help.

We have implemented the code and it did the trick.

Google now only index our non-www version. We are still in no-mansland in the SERP since June 16 but I guest it will take some time before we get our pre-June 16 ranking back.

10:49 am on July 10, 2005 (gmt 0)

New User

10+ Year Member

joined:June 29, 2005
posts:30
votes: 0


DumpedbyG,

What is the code that you finally used? I have the same problem as you have had, but I need to redirect from non www to www.

11:55 am on July 10, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 15, 2004
posts:89
votes: 0


"Thanks everybody for the help.
We have implemented the code and it did the trick.

Google now only index our non-www version. We are still in no-mansland in the SERP since June 16 but I guest it will take some time before we get our pre-June 16 ranking back."

Don't forget to test every url with the tool at

[webmasterworld.com...]

...even your main domain. When I first implemented my code, I found that is was also redirecting my main domain back to my main domain (a loop of sorts). The tool mentioned above found this for me, and allowed me to test until I had it all working correctly.

Dave

1:02 pm on July 10, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 13, 2005
posts:80
votes: 0


What is the code that you finally used?

Our code looks like this:

<?php
if (ereg("www", $_SERVER['HTTP_HOST'])) {
header("HTTP/1.1 301");
header("Location: [our-site.com");...]
exit;
}
?>

You can use it like this to redirect to your www version:

<?php
if (!ereg("www", $_SERVER['HTTP_HOST'])) {
header("HTTP/1.1 301");
header("Location: [your-site.com");...]
exit;
}
?>

Please note that this will only work on your home page and you will have to change the location URL for each page. I'm sure there is a more dynamic way to do this.

1:11 pm on July 10, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 13, 2005
posts:80
votes: 0


It's a functional equivalent of the mod_rewrite redirect - although not all files will be rewritten, only PHP ones (eg. not direct requests for images or static files).

We have added the following to our .htaccess file:

AddType application/x-httpd-php .htm .html

Which means we can use php code in .html files

10:20 pm on July 10, 2005 (gmt 0)

New User

10+ Year Member

joined:June 29, 2005
posts:30
votes: 0


DumpedbyG,

Thanks!