Handling 404 Error's With Apache - Implementing a 404 trap
(Page 3 of 4 )
Create a new file on your local machine and call it ".htaccess". If you're creating the file in notepad then make sure that you enclose the file name in double quotes as I have above otherwise it will be suffixed with a .txt extension, which will stop Apache recognising the htaccess file.
Apache has several re-write commands that can be used to overwrite Apaches default behaviour for several different events. To trap 404 errors, we re-write the ErrorDocument method. Place the following line into your htaccess file and save it:
ErrorDocument 404 404err.php
Believe it or not, that's the only line that we have to put in our htaccess file. The ErrorDocument keyword tells Apache that we're declaring a re-write for an error. The 404 tells Apache the error number that we want to trap, and the 404err.php tells Apache the name of the file that it should call when a 404 error occurs. You can use any file you like, but 404err.php is self-describing, so we will stick with it for now.
[Note] You can use the ErrorDocument keyword to trap several types of errors, and not just 404's. Here's a list of errors that you might also like to trap:
- 400: Apache doesn't understand the request the user is trying to make.
- 401: The user has failed authentication through an htaccess login.
- 403: The user is trying to access a forbidden area on the server.
- 500: An internal server error has occurred.
- 501: The requested feature is not implemented on the server.
[End Note]Once you've saved your htaccess file locally, upload/copy it to a directory on your web server. Before our error trapping will work, we need to create the file that will handle the 404 errors. Create a new file named 404err.php, enter the following code into it, and then upload it into the same directory that you placed the htacess file into:
<html>
<head>
<meta http-equiv="refresh" content="5;url=/index.php">
<style>
h3
{
font-family: Verdana;
}
body
{
font-family: Verdana;
font-size: 8pt;
}
</style>
<title> File Not Found: <?php echo $REQUEST_URI; ?> </title>
</head>
<body bgcolor="#FFFFFF">
<hr>
<h3>The file <?php echo $REQUEST_URI; ?> couldn't be found</h3>
Unfortunately the file you've requested doesn't exist.
Please click the button below to return to our home page.
<br><br>
<form action="/index.php">
<input type="submit" value="Continue »">
</form>
</body>
</html>Whenever PHP loads a web page, it also creates several variables that tell us the details of the visitor, their browser, the page they're viewing, etc. The $REQUEST_URI variable is defined internally by PHP and contains the path to the file that the visitor requested. For example, if I requested http://www.somesite.com/php/docs/bad.doc.html, then the $REQUEST_URI variable for that page would be /php/docs/bad.doc.html.
In our 404err.php file above, I've used a meta tag within the head of the document. This special meta tag redirects the visitor to /index.php after five seconds. The URI of the page that the visitor was after is shown in the browser, as well as a form button, that when clicked, will take the visitor back to our home page (assuming that our home page is index.php in the root directory).
Here's how the output looked when I tested the htaccess file and 404err.php script on one of the DevArticles Apache servers:

As you can, the 404err.php script gives a much clearer understanding of what's going on when compared to the standard 404 error, which is usually generated by the browser:

If you've integrated a search engine into your site then you might also want to include a search box in your 404err.php script. Many users will like the thought of being able to do a direct search from your 404 page, instead of having to be redirected back to the home page first.
[Note] If you're still presented with the standard 404 file not found error page even after you've uploaded your htaccess and 404err.php script, or you're presented with a totally different page, then there's a good chance that your host has already defined Apache's error handling methods for you. Often a simple email to your ISP telling him that you'd like to display your own error pages is enough to make them change Apache's configuration to allow directory-based htaccess awareness. [
End Note]Next: Conclusion >>
More Apache Articles
More By John Ferme