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.htmlErrorDocument 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.phpSo 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 FileBefore 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.
Next: Creating the PHP Error Document >>
More Apache Articles
More By