Don Havey

Sustainably harvested information

Archive for the 404 tag

Google and Custom 404 Error Pages  

I ran into this problem the other day:

After creating a custom error page handler for Curiobot that redirected users to the home page, I noticed that Google was not pleased. On the webmaster tools interface, Google said something like this:

We’ve detected that your 404 (file not found) error page returns a status of 200 (found successfully) in the header.

Here is what I had inserted into an .htaccess file:

ErrorDocument 404 http://www.curiobot.net/index.php?e=404

That would redirect any lost users to the home page, where an error message would be displayed. The problem was that the index.php page that I was redirecting to throws a status code of 200 (success) by default instead of 404 (error). So if Google didn’t specifically test for this, all URLs pointing to nonexistent pages on your site would look like they still functioned, and therefore be wrongly indexed.

Here’s the solution that I inserted into the index.php page that users are redirected to:

if(isset($_GET["e"])&&intval($_GET["e"])==404){
  header("HTTP/1.1 404 Not Found");
}

This forces PHP to send a 404 status code if a user has been redirected from a nonexistent page.
Hope this helps someone…

The article has

no responses yet

Written by Don

March 26th, 2008 at 11:35 am