Home arrow Apache arrow Page 2 - Trap And Get Notified: A Practical Solution To 404 Errors With PHP
APACHE

Trap And Get Notified: A Practical Solution To 404 Errors With PHP


Tired of seeing the same 404 errors in your log files? In this article Matt uses .htaccess and PHP to combat nasty 404 errors and get notified about them immediately as they occur.

Author Info:
By:
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
August 13, 2002
TABLE OF CONTENTS:
  1. · Trap And Get Notified: A Practical Solution To 404 Errors With PHP
  2. · Creating the .htaccess file
  3. · Creating the PHP Error Document
  4. · Uploading The Files
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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.
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