Classic 404 error pages are prone to being relatively useless. Whilst a well designed page can provide a means to find what they are looking for, wouldn’t it be great if you could find out more about what went wrong? This tutorial will show you how simple it is to have an explanatory email sent to you whenever a visitor hits a 404 page.
The first thing is to implement a custom 404 error page. We’ve covered this before, but it essentially involves adding the following to your root .htaccess file:
ErrorDocument 404 http://www.yoursite.com/404.php
This will instruct all 404 error requests to be sent to the specified page rather than showing a default message.
The PHP Code
The following code needs to be customized and placed anywhere in your 404 php page:
$myemail = “insert your email address here”;
$page = ‘http://’ . $SERVER_NAME . $REQUEST_URI;
$subject = “404 Page Error”;
$message = ”
404 Page Error Report
Visitor came from: “.$_SERVER[‘HTTP_REFERER’].”
Page not found: “.$page.”
This will send you an email which will include (if possible) both the page which the user has been referred from, and the page or resource which couldn’t be found.