Website error pages are perhaps one of the most overlooked pieces of a fully rounded website. Not only are they important but they give you the opportunity to have a little fun. Although many web developers rely on server logs to keep an eye out for hits on error pages, I’m going to take a different approach by using a PHP generated email. In addition, we will spice up the design a bit, add basic navigation and link to the website sitemap.
About Error Pages

The most common error page – the one in which you are most likely to be familiar with – is the “404 Not Found page”. More people encounter this type of error page than any other. Other common error messages you may have come across are 500 Internal Server Error, 400 Bad Request or 403 Forbidden. Wondering what the number is for? It simply refers to the HTTP code.

Default error pages are quite boring (as you can see above) and offer no purpose to visitors other than letting them know some boring error happened. For these reasons, it is a great idea to provide custom pages for the most common errors encountered. This tutorial will only cover two: the “404 Not Found” and “403 Forbidden”.
Check for custom error page support
First, check to make sure your hosting provider allows you to use your own error pages. Almost all of them do, and most of them even provide a configuration area within your control panel to help you quickly create the pages. In this tutorial we will configure an Apache web server (the most common). This is easier than you might think.
Configure .htaccess
Next, connect to your server via FTP or control panel and navigate to the document root directory (usually www or public_html) which contains your website files. We will be looking for the .htaccess file. It is sometimes hidden so make sure you are viewing all files including hidden ones. If your server doesn’t have one, you can create one using any text editor. Make sure to make a backup of the .htaccess file if your server already has one.
Add the following lines to your .htaccess file:
ErrorDocument 404 /error/404.php ErrorDocument 403 /error/403.php
The first half (ErrorDocument 404) is telling the server we are going to define the location of the 404 error document. The second half defines the actual location of the error document. In this case we will put it in the “error” directory and call them 404.php and 403.php, respectively.
Now save the .htaccess file and upload it to the document root directory.
Design the Custom Error Pages
It is best to stay with the same design as your website already uses so that you don’t confuse your visitors and risk losing them. You should also include helpful elements such as a polite error message, suggested links, a search feature, or a link to your sitemap. These features will depend on the level of content your website provides and what you feel will be most helpful.
As you can see below, the 404 Not Found page for Nettuts+ has stated the error and emphasized the search feature by including it in the body beneath the error message. You could take this a step further by including a short list of links to possible pages which might encourage the visitor to continue exploring more of the site (keep it simple and short though) -or even a humorous image (every one likes laughing right?). For small websites it may be a good idea to include a visible sitemap as well.

Here is something I put together for this tutorial that you can use for your website as well (included in the download above). It’s very simple so you will be able to put the content of it directly into your existing website template. As you can see, I attempted to include a little bit of a humorous element while also stating the error politely and including some options to help the visitor either find what they were looking for or continue browsing the website.

You’ll notice it does not specify the HTTP error code in the body of the page. Instead I chose to only use the error code in the title of the page. The reason for this is to keep things as simple and user friendly as possible. Most people don’t care what 404 or 403 means, they want to know what’s going on in plain English. For people who want the error code, it is still available via the title.
If you want to see some really great 404 designs visit:
- http://www.smashingmagazine.com/2009/01/29/404-error-pages-reloaded-2/
- http://www.smashingmagazine.com/2007/08/17/404-error-pages-reloaded/
- http://www.smashingmagazine.com/2007/07/25/wanted-your-404-error-pages/
- http://blogof.francescomugnai.com/2008/08/the-100-most-funny-and-unusual-404-error-pages/
The Auto-Mailer PHP and Why We Will Use Email Notification
This is the part of the tutorial in which some web guru’s might argue with. You can use your web server’s logs to check for error pages and much, much more. Why do I choose email notifications?
- I don’t want to log into my server every day and dig through all that extra information.
- I am available by email almost literally all day, the fastest way to reach me is email (or twitter). With this in mind, I want to know about 404 and 403 errors fairly quick so email is best.
- An increasing number of people are starting websites, while most of those people know almost nothing about web hosting let alone server logs. These people will only be running small sites; so email is ideal.
- Being notified right away allows me to quickly take action if a website of mine is being “harvested” (ThemeForest templates), if someone is attempting to access something restricted repeatedly or if I have a broken link somewhere.
So with all that said, let’s get on with the code shall we!
The Code
First, we will create a file named error-mailer.php which will be used to collect information about our visitor and send the email. Once you have created the file we will start by specifying our email and email settings.
<?php # The email address to send to $to_email = 'YOUR-EMAIL@DOMAIN.com'; # The subject of the email, currently set as 404 Not Found Error or 403 Forbidden Error $email_subject = $error_code.' Error'; # The email address you want the error to appear from $from_email = 'FROM-EMAIL@DOMAIN.COM'; # Who or where you want the error to appear from $from_name = 'YourDomainName.com';
Then we will collect information about our visitor such as IP address, requested URI, User Agent, etc. The following code will collect that information.
# Gather visitor information
$ip = getenv ("REMOTE_ADDR"); // IP Address
$server_name = getenv ("SERVER_NAME"); // Server Name
$request_uri = getenv ("REQUEST_URI"); // Requested URI
$http_ref = getenv ("HTTP_REFERER"); // HTTP Referer
$http_agent = getenv ("HTTP_USER_AGENT"); // User Agent
$error_date = date("D M j Y g:i:s a T"); // Error Date
Now we will write the script to email the information to us with the details specified earlier.
# Send the email notification
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $from_email;
$mail->FromName = $from_name;
$mail->Subject = $email_subject;
$mail->AddAddress($to_email);
$mail->Body =
"There was a ".$error_code." error on the ".$server_name." domain".
"\n\nDetails\n----------------------------------------------------------------------".
"\nWhen: ".$error_date.
"\n(Who) IP Address: ".$ip.
"\n(What) Tried to Access: http://".$server_name.$request_uri.
"\n(From where) HTTP Referer: ".$http_ref.
"\n\nUser Agent: ".$http_agent;
$mail->Send();
?>
We are using the phpMailer class to do this as demonstrated by Jeffrey via the ThemeForest blog to create a nice AJAX contact form. This version of the phpMailer class is for PHP 5/6 so if your server is running PHP 4 you will need to use the corresponding version by downloading it here.
404.php and 403.php Error Pages
The last thing we need to do is customize the error pages we designed earlier by sending the proper headers and set the $error_code variable by inserting the following code at the beginning of each page respectively (separated by ——-).
<
<?php header(“HTTP/1.0 404 Not Found”); $error_code = ’404 Not Found’; // Specify the error code require_once(‘error-mailer.php’); // Include the error mailer script ?> ——- <?php header(“HTTP/1.0 403 Forbidden”); $error_code = ’403 Forbidden’; // Specify the error code require_once(‘error-mailer.php’); // Include the error mailer script ?>
What we are doing here first is setting the correct HTTP header to return 404 Not Found and 403 Forbidden, respectively. When search engines accidentally land on this page we want to make sure they know what kind of page it is, instead of thinking that it's a normal web page named 404.php or 403.php.
Then we specify the error code to be used in the mailer script and include the mailer script so it can do its work. This way if we make a change to the mailer script, we only need to edit one file instead of two or more (if you setup additional custom error pages).
Conclusion
There you have it! Your own custom error pages that are search engine friendly, and let you know via email when you've had a visitor as well as all the information you will need to fix any problems. A few last things to consider:
- Internet Explorer requires error pages that are at least 512 byes in size (if you use the example files you'll be fine)
- High traffic websites have the potential to generate A LOT of emails so make sure you setup some sort of email filter for these error notifications so they don't flood your inbox. I use Gmail so I just have a label and filter setup for these emails.
- Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Nice work, keep it up!!!
Nice work Keep it Up
Thanks for the tutorial, very handy.
Good, even i have implemented same in my previous company but how about having all the error(404,403,500) redirected to only one error.php and in the common error.php page we will handle in switch, basically pass the code via .htaccess error.php?code=404, error.php?code=403 etc.
If you were implementing this on a more trafficked site wouldn’t it make more sense to only send a notification for every 1 in 5 pages views to save email storage and server capacity?
Nice tutorial.
Thanks, great article.
very nice script. But i am having some problems with it. i downloaded the source code and extract it to folder on my computer, i modified the error_mailer.php adding an email address where it said “email address to send to” i left everything else untouch for now.
should i upload all the files in the folder i create to my server or should i upload all the files out of a folder in my root directory.
should i put the 404.php and 403.php in a separate folder “name error” i got this from my error log
[Wed Sep 09 16:13:36 2009] [error] [client IP Address "i remove this"] File does not exist: /home/thecomp2/public_html/404.shtml, referer: http://www.thecomputershop.me.uk/
can you please instruct me in which way i should upload these files.
i got the 500 internal server error so i when back and deleted the text from the .htaccess file (errorDocment 404 /error/404.php and 403.php) and then try my url again and my website came up perfectly. So i put the text back in the .htaccess file and i got the 500 internal server error could there a problem with the location of the 404.php/403.php files. please help me i spent over 6 hours trying to figure it out and i still haven’t got a clue
fine information
It’s cool…
Custom error pages are a great way to make even the most basic site seem polished – you just need to make sure to always send the appropriate headers.
Thanks for the great tutorial!
very helpfull information…even a begnner can understand…thanksss
I have an issue, the script works fine otherwise. However, the mail which I get when someone types a non existing page is as under and you can see the Tried to access page is the error page itself and not what the visitor tried to access also the HTTP referer is missing which I did feed as the domain:
———————————————————————————————————————————-
There was a 404 Not Found error on the http://www.mysite.com domain
Details
———————————————————————-
When: Sun May 23 2010 9:48:48 pm CDT
(Who) IP Address: XX.XX.133.32
(What) Tried to Access: http://mysite.com.404.php
(From where) HTTP Referer:
User Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
———————————————————————————————————————————-
I find this same problem,
The mail which I get when someone types a non existing page is as under and you can see the Tried to access page is the error page itself
When: Sun May 23 2010 9:48:48 pm CDT
(Who) IP Address: XX.XX.133.32
(What) Tried to Access: http://mysite.com.404.php
(From where) HTTP Referer:
Can you help me fix this?
Love and kis to all of you :)
Ludmilla
the sales of these cars are all of the world
Very good info which I have not known before..Nice Web Design Error Pages too.. thanks for the post..
Add more good web Design Error Pages..Thanks for the post..
I have the same problem as Prosenjeet and ludmilla
Tried to access page is the error page itself and not what the visitor tried to access, I have this same problem, If this can be fixed, it is a very nice script, if not, I don’t see the point in the e-mail funktion, can someone help?
As I have reported in my earlier post, the error reporting doesnt work and it has been confirmed by morten B and ludmilla.
Any fix to that please. Else this is simply a 404 and 403 script that can be achieved by .htaccess easily, why take pains installing this?
The Code works great, but my error logs show the following… it happens 3 times (same time stamp) every 50 seconds or so. Any ideas what it is?
[Thu Dec 16 22:49:36 2010] [warn] Cannot get media type from ‘php-script’
Additionally, I always get 2 emails for every error that occurs. They are time stamped a few seconds after eachother. Does the script send a email to both the $to_email and $from_email ?
some simple code to handle all of the things below easily in one php file:
http://http://www.alanhart.co.uk/archives/2011/09/19/single-php-error-page-to-handle-all-response-headers-errors/
1. Error page generated by dynamic pages (using any code; 403, 404 etc)
2. Error page to handle genuine errors (404 ‘Not Found’, 403 ‘Forbidden’ etc)
3. Forbid visitors from visiting sensitive areas/directories also returning 403 ‘forbidden’ header.
(e.g. /images, /css /javascript, /ajax etc)
fine information, will be used, thanks!
Hello,
Nice tutorial, I wanted to ask if I have my files split int header and footer and place the code in the header.php of the page how can I identify the page user wanted to access?
Thanks for your help.
Really nice post, this is a viewpoint that a colleague and I most definitely share, there are far to many web designers in the industry that completely overlook the error pages, just bad practice in my opinion.