Custom 404 errors

see some examples of 404 pages, and of course the gallery

404 is the error you get when a document you requested wasn't available on the server. If you haven't mistyped the name, it's normally beacuse the page was moved, renamed, or the site was deleted

The best defense against your pages going missing then, is to have a website which returns what the users want. For example:

Custom 404 messages - the basics

It's simple. Add the following line to a file called .htaccess in the root directory of your website:

ErrorDocument 404 /Help/NotFound.htm

(note the dot at the beginning of the filename. You might not be able to see the file when it's there unless you set your FTP client to display hidden files

Of course, you then need to create a page and put it at /Help/NotFound.htm or wherever

What to write

As with everything to do with websites, sit down and think about what the user needs. They're trying to find a page, and it's failed. What to do next?

They might be new to your website, and might benefit from starting at the front page. If the website is simple this should get them where they need to be. But on big websites it can take a while for them to get from the front page to a specific item, and they'll want to speed up that process with a search function

So a link to your search engine would work. Or if you don't have one, a link to google with instructions for how to search your site

Careful not to litter the screen with information. Look at microsoft.com's 404 page* for an example. People are already feeling confused because they can't find something, best not to confuse them more with lots of choices. Just get them to the content they were seeking before they leave your site for good.

* of course, you don't want to emulate their use of invalid characters in a page title either, at least not without specifying the right character-set for your page...

And of course, a feedback link. If the error came from following a broken link in your site, then you need to fix it, and the person viewing the error message knows you need to fix it. Provide some way for them to let you know about it. If nothing else, it'll give them someone to write to for help, and when they get your reply in a couple of days with the correct URL, they get reminded to go back to your site again.

Other features depend on having scripting enabled in the error page for the truly intelligent 404 responses, so more on that below

You enter room 404.
It is pitch black. You are likely to be eaten by a grue

OK>_

Getting the 404 page to run script

So far as I can tell, apache likes the error documents to be plain HTML, and it isn't happy about redirecting to a page with a PHP extension. So try the following line in an .htaccess file (put this file in the same directory as your error document)

AddType application/x-httpd-php .htm

That will tell your webserver to run PHP scripts, even if they're inside files with the .htm extension

Useful things to do with script

An intelligent search

<?php
    $SearchText = urldecode($_SERVER["REQUEST_URI"]);
    $Terms = preg_split("/\W+/", $SearchText, 10);
    $Query = implode("+", $Terms);
    $Query2 = implode(" ", $Terms);
    $URL = "http://www.google.co.uk/search?q=$Query+site:www.blibbleblobble.co.uk";
    print "<p><a href=\"$URL\">try this google search</a></p>\n";
  ?>

That's the code for my website, you just need to change blibbleblobble with the domain name of your website to get it to search your own site

Report the error

if($fp = fopen($DOCUMENT_ROOT . "Filename/log.tx", "a"))
{
  fwrite($fp, urldecode($_SERVER["REQUEST_URI"]) . "\n");
  fclose($fp);
}

That logs the name of misspelled pages to a text file, and could be easily adapted to report to a MySQL server instead. Remember that people will be able to add any text they like to the URL, so make sure whatever system you use to read it can't be compromised by people typing http://www.mysite.com/");drop+table+404;-- into their browsers