'Not Found' pages returning 200 response codes

What's the problem?

We love to store web page content in databases and we love to create a nice 'Not Found' page for when a visitor decides to add something to the page ID in the URL, or otherwise play around with it, or a page has been removed for whatever reason.

It's amazing how many of these 'Not Found' pages actually return a HTTP 200 response code. The 200 response code means OK, we have found the content you're looking for. This means web crawlers such as the Googlebot will consider indexing the page's content. Google does not like to keep custom 'Not Found' pages in its index, so this will have a detrimental impact on your SEO (Search Engine Optimisation).

The 404 HTTP response code

We've all seen '404 not found' pages. This response code means the content was not found. Whenever we're displaying a 'Not Found', for example in a CMS (Content Management System) style environment, there needs to be a 404 response code for that request. The reason for this is so that the Googlebot (and others) know that the content was not found, and not to index the page.

In terms of accessibility, the 404 code can be helpful with for example specialist web browsers such as screen readers because depending on the configuration, the screen reader may have a special routine for dealing with 404's.

In PHP, the response code can be modified using the header() function.

PHP Code <?php

    header
('HTTP/1.0 404 Not Found');

?>

In the above example, we send a 404 response code to the browser. Normally the remaining page content will still be shown to the user, unless they have configured their browser otherwise. So you can still have a nice 'Not Found' page, but serve the proper response code aswell.

A note on the header() function

Headers can only be sent to the browser before any actual page content is sent. Due to nature of the HTTP protocol, you can't send any page content followed by any headers. PHP will raise an error along the lines of:

Cannot modify header information - headers already sent by...

Even a whitespace character at the beginning of your page will trigger this error because even though it may be a single space, it will still be treated as page content and sent to the browser.

Comments

There are 0 responses. Why not add a comment? No registration required.

Leave a Comment

Enter Code

Refresh code

Menu

Firefox Addons