Apache
  Home arrow Apache arrow Page 2 - Trap And Get Notified: A Practical Solutio...
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

Trap And Get Notified: A Practical Solution To 404 Errors With PHP
By:
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2002-08-13

    Table of Contents:
  • Trap And Get Notified: A Practical Solution To 404 Errors With PHP
  • Creating the .htaccess file
  • Creating the PHP Error Document
  • Uploading The Files
  • 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


    Trap And Get Notified: A Practical Solution To 404 Errors With PHP - Creating the .htaccess file


    (Page 2 of 5 )

    Using a text editor such as notepad, create a new file and save it as ".htaccess" – make sure the quotes are included as this will then create a .htaccess file without any file extension. If this does not work, don't panic! Simply save the file as "htaccess.txt" and it can be renamed once uploaded.

    Within the .htaccess file, an ErrorDocument directive is used. This has the same format for each error type and tells PHP which page to show when a specific error occurs. Its syntax looks like this:

    ErrorDocument errornumber /somefile.html

    ErrorDocument remains the same and obviously errornumber is substituted for the error number, such as 404, 403, etc (the most common error numbers will be given at the end of this article). Somefile.html is the file that will be displayed to the user once the error has been invoked. This file can be any file format which the browser can understand, such as ASP, HTML or PHP (which we will be using in this tutorial).

    Make sure you have the .htaccess file you've just created open, and type the following line:

    ErrorDocument 404 /404.php

    So when a 404 error (page not found) occurs, the .htaccess file will display the 404.php page and you wont see the default ugly browser error page.

    Creating the Configuration File
    Before we create the main error page we need to specify a few variables that will be used in all of our error pages. A configuration file will store the variables used throughout the error pages. For example, if you change your email address and want the error report emails sent to the new one, then you only need to change it in the config file, rather than in every single error document.

    Using your favourite web editor, create a new file called "config.php". Firstly we are going to enter the email address to notify when the error occurs. Type the following into the editor:

    <?php

    // who is the mail going to
    $mailto = "you@yourdomain.com";

    ?>


    Next, we are going to specify your website location. This is used on all of the error pages to send the user back to your homepage. If you want to send the user to a different page, you can change that URL here. Add the following line under the $mailto line, which we just added:

    // the URL the user will click on to leave the error page
    $url = "http://www.yourdomain.com/";


    We will also need to say from whom the email has been sent, otherwise it will be blank. This doesn't matter too much; as you will not be replying to it, so simply name it something like this:

    // who is the error email from
    $from = "report@yourdomain.com";


    Your code should look something like this:

    <?php

    // who is the mail going to
    $mailto = "you@yourdomain.com";

    // the URL the user will click on to leave the error page
    $url = "http://www.yourdomain.com/";

    // who is the error email from
    $from = "report@yourdomain.com";

    ?>


    Save the file and close it. We're now going to create the actual error document.

    More Apache Articles
    More By


     

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek