A Smart 404 Page in PHP

Wednesday, April 15, 2009

This blog entry is technical, and intended for people who own websites that use PHP. This way, I can alienate everyone who reads my blog and end up with zero readers. Consider this risk a public service. The rest of you can catch the next one.

Now, imagine you’re happily browsing a website, when bang, you’ve just navigated to a page that doesn’t exist. What do you do? Do you freak out? Well, if you’re the hardened programmer this blog post expects you to be, you should know what you’re doing. But do you expect your end users to know what to do?

Hopefully the happy 404 page is helpful enough to get you to where you need to be. Unfortunately, very few of them are. Many of them are just overly templeted “Page Not Found”, which many people don’t understand.

I requested four-hundred and forty pages? And none of them could be found?!

 

Worse yet, even if they do know what went down, and understand that “Oops… the page you requested could not be found on the server because it just doesn’t exist”, many 404 pages don’t help bail the user to where they want to go. At best, the 404 page will tell them to use the back button, or give them a link to the homepage.

The back button! Of course! Why didn’t I think of that? I’ve never used that thing to browse before, but maybe I can now! Thanks, Mr. 404 Page!

 

Enter the Smart 404 Page

So here’s what we do. Warning: Actual code below!

This sets you up with the data you need to do pretty much anything you want.

<?
$refferer = @$HTTP_REFERER; //Gets the page that sent you to the 404.
$page = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //Gets you the page you requested prior to the 404 redirect.

 

We’re going to make a value to display. First is going to display some boilerplate text about what happened.

$text = "<p>Sorry! We know you were looking for a webpage, but instead you reached boilerplate text that is describing a 404 error on a level that even computer illiterates can understand. The reason for your problem is that you ";

 

Then you can program for certain eventualities.

Here’s an example of something you may want to say if you’re requesting a certain page. The code checks to see if you made the request and then appends the explanation onto the text.

if (strstr($page, "http://www.greatplay.net/raptors")) { $text .= "were looking for the Velociraptor page that was discontinued after we got sued.</p>" }

 

The incorrect typing, such as misspelling a link, is the most common. Going to greatplay.net/gamess will result in this happening:

else if (!$refferer) { $text .= "tried to click on a bookmark or type in the URL manually and did not do so correctly. Maybe you misspelled something.</p>"; }

 

Here’s how you code and explain it if the problem was on your end:

else if (strstr($refferer, "http://www.greatplay.net/")) { $text .= "clicking on a broken link that is 100% on my own website and 100% my own fault."; }

 

And if it’s on someone else’s end, you put this in to finish if-then-else statement tagging:

else { $text .= "clicking on a broken link contained on $referrer. I recommend you go to their website and contact their website and yell at them loudly until they fix the problem."; }

 

And then you wrap up the script!

$text .= "</p>
$text .= "<p>Thank you and don't fall into a 404 again!</p>";
echo $text;
?>
<a href="http://www.greatplay.net">Back to homepage »</a>

 

And that’s the script! Optional formatting to match your website or to make it look moderately presentable is left as an exercise to the reader.

Be Sociable, Share!

 

Liked this Essay?

5 Comments (RSS)

Read them below and add one yourself.

  1. Anonymous says:

    how come i enabled javascript but it didn’t work?

  2. Hi,

    Thanks for the code Peter, it looks good :)

    However I’m after something a little different if you can help? Basically I want to have what you have coded above but with a search function and a list of related pages to the visitors search.

    For example. I own a hosting site so if someone goes to mysite.com/game-servers and that page dont exist, I would like to show messages like above plus a site search function along with a list of pages related to the words in the url, with a live link and snippet of text from that pages first paragraph, so it would show say:
    “mysite.com/game-servers.php
    We have a large selection of popular game servers at low cost in the UK…

    mysite.com/supported-games.php
    We support a number of well know games hosted in the UK…

    mysite.com/our-game-servers.php
    This is a list of our game servers currently online…”

    And so on for any url on my site related to game servers.

    Do you get what I mean? And how hard do you think that would be to code so I can use it on any site?

  3. Peter says:

    Nathan, you might be interested in the WordPress Smart 404 plugin at http://atastypixel.com/blog/wordpress/plugins/smart-404/

    That comes fairly close to what you wish, as it redirects any 404 to a close match, if there is one.

    I’m sure that what you specify is indeed possible if you have WordPress, but I wouldn’t immediately know how to do so, and don’t really have the time at the moment. Perhaps there is a plugin for it that I’m not aware of, though.

  4. Peter says:

    Nathan, I did a search and I found a Worpress plugin called “AskApache Google 404″ available at http://www.askapache.com/seo/404-google-wordpress-plugin.html that does pretty much exactly what you are looking for.

    – Peter

Leave a Reply

Comment HTML: You can use HTML in comments. I reccomend <blockquote>Quote</blockquote> for quoting what others have said. <b>Text</b> is for bold, <i>Text</i> is for italic, and <a href="url">text</a> is for making links.