Home arrow Apache arrow Page 3 - Handling 404 Error's With Apache
APACHE

Handling 404 Error's With Apache


The web is all about usability, consistency and speed... well, that's what we'd like to think anyway. In this article John shows us how to stop those dreaded 404 errors from plaguing our Apach-based web sites. With a bit of PHP and a htaccess file or two, your site will be free of 404's forever!

Author Info:
By: John Ferme
Rating: 4 stars4 stars4 stars4 stars4 stars / 9
February 20, 2002
TABLE OF CONTENTS:
  1. · Handling 404 Error's With Apache
  2. · Apache and hierarchies
  3. · Implementing a 404 trap
  4. · Conclusion

print this article
SEARCH DEVARTICLES

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:

Testing our htaccess and 404err.php scripts

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:

Internet Explorers default 404 page

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]
blog comments powered by Disqus
APACHE ARTICLES

- Programmatically Manipulating Microsoft Exce...
- Installing PHP under Windows
- Compressing Web Content with mod_gzip and mo...
- Compressing Web Output Using mod_deflate and...
- Setting Up Apache 2.0.45 to Parse PHP Pages
- Custom Error 404 Documents with PHP
- Using Apache and PHP on Mac OS X
- ASP: Active Sessions, Active Logins and Tota...
- Working With Oracle on Windows: Part 1
- The Quick-n-Dirty Guide to Setting Up Apache...
- Installing Apache With SSL: The Complete Gui...
- 7 Powerful .htaccess Customization Tips
- Trap And Get Notified: A Practical Solution ...
- One Way To Use Server Side Includes
- Using ForceType For Nicer Page URLs

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials