homepage Welcome to WebmasterWorld Guest from 54.237.99.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Google / Google SEO News and Discussion
Forum Library, Charter, Moderators: Robert Charlton & aakk9999 & brotherhood of lan & goodroi

Google SEO News and Discussion Forum

    
php header 301 and Google
DumpedbyG

5+ Year Member



 
Msg#: 30292 posted 6:56 pm on Jul 7, 2005 (gmt 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?

 

Wizard

5+ Year Member



 
Msg#: 30292 posted 8:37 pm on Jul 8, 2005 (gmt 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.

Marval

10+ Year Member



 
Msg#: 30292 posted 10:21 am on Jul 9, 2005 (gmt 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...]

Vulcan315

10+ Year Member



 
Msg#: 30292 posted 12:05 pm on Jul 9, 2005 (gmt 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

SebastianX

10+ Year Member



 
Msg#: 30292 posted 12:06 pm on Jul 9, 2005 (gmt 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.

encyclo

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



 
Msg#: 30292 posted 12:55 pm on Jul 9, 2005 (gmt 0)

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.

Wizard

5+ Year Member



 
Msg#: 30292 posted 6:20 pm on Jul 9, 2005 (gmt 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.

DumpedbyG

5+ Year Member



 
Msg#: 30292 posted 9:43 am on Jul 10, 2005 (gmt 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.

John_1976

5+ Year Member



 
Msg#: 30292 posted 10:49 am on Jul 10, 2005 (gmt 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.

Vulcan315

10+ Year Member



 
Msg#: 30292 posted 11:55 am on Jul 10, 2005 (gmt 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

DumpedbyG

5+ Year Member



 
Msg#: 30292 posted 1:02 pm on Jul 10, 2005 (gmt 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.

DumpedbyG

5+ Year Member



 
Msg#: 30292 posted 1:11 pm on Jul 10, 2005 (gmt 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

John_1976

5+ Year Member



 
Msg#: 30292 posted 10:20 pm on Jul 10, 2005 (gmt 0)

DumpedbyG,

Thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Google / Google SEO News and Discussion
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved