Apache
  Home arrow Apache arrow Page 3 - Handling 404 Error's With Apache
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
APACHE

Handling 404 Error's With Apache
By: John Ferme
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2002-02-20

    Table of Contents:
  • Handling 404 Error's With Apache
  • Apache and hierarchies
  • Implementing a 404 trap
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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]

    More Apache Articles
    More By John Ferme


     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek