Code Samples,Search Engine Optimization

How to Create 404 Page Not Found Custom Error Pages in PHP

15 Aug , 2008  

Sexy Girl Motioning Stop, Page Not Found 404Google’s Official Blog has been talking about 404 server response codes (Page Not Found errors) this week, and since I just wrote about 301 Redirect Responses last week I guess I might as well pick up where they left off and describe how to handle a custom 404 message with a PHP page.  As a web developer and an SEO it’s important to understand how web servers work, as it can impact both site performance for your users and for search engines.

Whether you are hosting your website on a Windows IIS Server or a Linux Apache Server (or Unix, or Mac, etc.), the essential job of the web server remains the same.  The web server listens for a network request on port 80 and when it receives one, it sends a response.  When you type a URL into your browser, let’s give a shameless plug to myself and say http://www.barrywise.com, a network request is sent on port 80 to whomever is hosting that domain.  In this case it’s my URL so my web server is sitting here all day waiting for your request.  Really.  All day.  Before my web server actually sends my home page back to your browser to display it for you to read, it first sends a Server Response Code in the HTTP Header.  This code tells your browser what to do with the page.

Most of the time you’ll get a 200 code back, which means all is OK, and the browser simply displays the web page. If you typed in a URL of a page which doesn’t exist, however, the web server sends back a 404 code which tells your browser that it could not find that page on the server.

But there’s a problem with the way some CMS software, and even web designers, handle Page Not Found errors.  Suppose you don’t want to tell your site visitors that a page is not found.  You’d rather send them a different page instead, maybe with alternate content on it or some advertising banners like Google Adsense.  In this case, you may wish to alter the way your web server handles 404s and just redirect the request to an alternate content page with a 301 status code, or worse yet deliver a 200 code, meaning all is OK with the page.  This is called a soft 404 and Google doesn’t like it.

WordPress does correctly handle 404s, but there are some other CMS’s which don’t.

If you’d like to know how your server is handling them, just check a header status code tool. It will show you the exact response code your server is returning. You want to see 404 for pages which are not there, 200 for normal OK pages, and 301 for any pages you want to redirect with a rewrite rule.

The reason search engines hate these soft 404s is because they can’t accurately crawl your correct URL structure.  They get false positives on pages which don’t actually exist.  Since your server returned a status code of 301 or 200, the search engine thinks the page exists, even though users reading the page can tell otherwise. 

So how do you correctly handle 404 Page Not Found Errors?

Make sure you are not redirecting Page Not Found errors, and use custom 404 messages instead.  With Windows you can display a custom ASP or ASP.NET page, and with Linux/Apache you can have a custom PHP page display for all 404 errors.  Then you can process the requested file name/URL and generate custom messages for your 404 errors.  For example, in Apache, just edit your httpd.conf file and add this:

ErrorDocument 404 /pagenotfound.php

Then create a PHP file called pagenotfound.php and use getenv(“REQUEST_URI”) to obtain the page which the visitor was trying to view.

For example, let’s say someone mistyped your web address and entered http://www.yoursite.com/pagg.php, instead of http://www.yoursite.com/page.php.  Your web server will return a status code of 404, Page Not Found, telling search engines the page isn’t there, but the actual page displayed to the user will be the file you created called pagenotfound.php.  Now in that PHP page, you can place something like this code:

Sorry, you were looking for <?php=getenv("REQUEST_URI")?>, but we couldn't find that page. Are one of these other pages what you were looking for:

To the end user, this will look like:

Sorry, you were looking for pagg.php, but we couldn't find that page. Are one of these other pages what you were looking for:

And then of course you can list several alternates for the visitor, be it other content pages or advertising banners, etc.  You can even have the server email you every time someone gets a 404 message on your website, if you want to micromanage the whole thing.

, , , , , , , , ,


8 Responses

  1. XIII says:

    Hmm, so the way WordPress handles this by default for example, is wrong? Or correct?
    Which CMS does this correctly and which doesn’t?

  2. Barry Wise says:

    I should have specified that further; WordPress does correctly handle 404s, but there are some which don’t.

    If you’d like to know how your server is handling them, just check a status code tool, like this one: http://www.seoconsultants.com/tools/headers.asp

    It will show you the exact response code your server is returning. You want to see 404 for pages which are not there, 200 for normal OK pages, and 301 for any pages you want to redirect with a rewrite rule.

  3. g1smd says:

    The Live HTTP Headers extension for Mozilla Seamonkey or for Mozilla Firefox is also very useful.

  4. HIP says:

    I have a blog with custom 404 through wordpress though do need to sort this for old pages. Done the redirects though could do with a 404 page.

  5. Barry Wise says:

    Using proper 404s and 301s are both essential, it’s worth the time to set them up.

  6. Bitstar says:

    Unfortunatly some hosting companies don’t let you handle custom 404 errors in your website. I mean the cheap ones. They like to shove some ads in the user’s face instead.

    I can however understand that, because our goal here is the same. Even if the user didn’t find the right content we want to keep him on the website and perhaps show his some ads or whatever.

  7. Steve says:

    check out google webmaster 404’s with the new script to generate links on your 404’s of related content in your domain

  8. meddyniho says:

    where do i create pagenotfound.php file .. i created this file in /etc/apache2/ directory .. plz help .. thankyou :)

Comments are closed.

How to Create 404 Page Not Found Custom Error Pages in PHP

by Barry Wise time to read: 3 min
8