404 Not Found Error Emails

     

The 404 not found error notice is something webmasters hate. But the fact is broken HTML and image links do occur. Here is how to get real-time email notices when they do occur.

Yes, I know log files give details of errors. But how often do you check your log files? Unless you examine them every day, it could be weeks even months before you discover the 404 not found errors. By then hundreds, even thousands of visitors may have left your site never to return, nevermind the lost advertising revenue or sales, because they ended up on a 404 not found error page.

196 404 Not Found Error Messages

Recently I encountered this problem when I made some changes to an existing website. I set up a 404 not found error email notification system, and immediately my mailbox was flooded with reports of broken links, search engine bots trying to crawl old nonexistent files, broken image links and missing favicon images.

I was shocked just how many error messages I received - 196 404 not found error messages in 24 hours. It even caught an issue with one of my affiliate campaigns. If I didn't catch that, my AdWords campaign would have sent traffic to a non-existent URL. It could have ended up costing me thousands of dollars in wasted advertising and lost sales.

How to Set Up Your Own 404 Not Found Error Email System

If you want to take a proactive approach and get real-time notices of 404 not found error messages as they happen, then you should set up your own real-time 404 not found error email notification system.

Here's how I did it: By the way, your server must have PHP enabled otherwise this method doesn't work. Ask your server administrator whether your server is PHP enabled and if it isn't, then ask them to enable it.

Also remember to backup any files you intend to edit. If something goes wrong, you can always revert back to the previous file.

The system uses two files; the .htaccess file which tells the server where to send users when there is an error, and the 404 not found error file which sends the 404 not found error email notices to you.

How to Create a .Htaccess File

A '.htaccess file' (hypertext access) is the default name of a directory-level configuration file. If you don't have a .htaccess file in the main directory of your server where you upload your files, create one by opening up a blank file in your favorite text editor and add these 3 lines of code:

ErrorDocument 401 /404.php
ErrorDocument 404 /404.php
ErrorDocument 500 /404.php

Definitions:

  • Error 401: Unauthorized - This error appears when a visitor tries to enter a part of the site they are not authorized to access.
  • Error 404: Not Found - This error appears when a visitor accesses an incorrect URL and the server cannot access it.
  • Error 500: Internal Server Error - This error is usually caused by CGI in which the script used in the process does not work properly.

Save the file as '.htaccess' (a period followed by 'htaccess', without quotes. There's nothing after htaccess)

How to View Hidden Files

If you can't see the .htaccess file in Windows Explorer or Mac Finder, you have to enable the option to view hidden files.

On the Mac, open up Terminal and type this:

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

When it come to Windows users, there are different ways to enable hidden files depending on which version of Windows you have. I suggest you follow the instructions at How to see hidden files in Windows.

How to Create a 404 Not Found Error Email Notification File

Create a blank file with your text editor and add the following code:

<html>
<head><title>404 Not Found Error</title>
</head><body>
<h1>Sorry!</h1>
<table width="60%" align="center"><tr><td>Sorry but we couldn't find what you're looking for! Don't panic - it's not your fault. We have been notified of the error.</p>
<p>Here are a few options to find what you're looking for:</p>
<ul>
<li>Double check the web address for typos
<li>Click the 'BACK' button in your web browser
<li>Search the site using the search form above
<li>Go to the <a href="http://www.yourdomain.com">homepage</a> (replace yourdomain.com with your website's homepage)
</td></tr></table>
<?php
$ip = getenv ("REMOTE_ADDR");
$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$combine = $ip . " tried to load " ;
$url = $servname . $requri ;
$httpref = getenv ("HTTP_REFERER");
$httpagent = getenv ("HTTP_USER_AGENT");
$today = date("D M j Y g:i:s a T");
$message2 = "On $today, $combine:\n
http://www.$url\n
User Agent = $httpagent \n
$httpref ";
$to = "email@yourdomain.com";
$subject = "404 Error Report";
$from = "From:email@yourdomain.com\r\n";
mail($to, $subject, $message2, $from);
?>
</body>
</html>

I'm not a programmer so I borrowed the PHP code from Free Custom 404 Error php script with htaccess tutorial n block folder script. Thanks IBD Host!

Change 'email@yourdomain.com' to your email address and save the file as '404.php'. Now upload both the .htaccess and 404.php files to your main directory on your server. Test it by visiting a URL that doesn't exist on your server. You should receive a 404 not found error email notice in your mailbox within a few minutes.

How to Redirect Broken/Missing Links

So what do you do with broken links? First, you fix it if possible. If that isn't possible, add a 'Redirect' command to your .htaccess file like so:

Redirect /brokenlink.html http://www.domain.com/redirectedfile.html

The code above contains 3 parts. The first part is the word 'Redirect', followed by a space, then a forward slash, then the broken/missing URL, followed by another space, then the URL you would like to redirect the user to.

Note that the broken/missing URL must be a local URL (meaning on the same domain). The redirected URL does not have to be on the same domain as the missing URL.

How to Set Up One 404 Not Found Error Webpage for Multiple Domains

I have quite a few web sites and didn't want to have to maintain a 404 not found error webpage on each site. It is far easier to maintain just one 404 webpage. So I came up with this solution.

I set up the 404 not found error email notification file using the code below. Remember to create a blank file with your text editor and add the following code:

<html>
<head><title>404 Not Found Error</title>
</head><body onload=setTimeout("location.href='http://www.redirecteddomain.com/404.html'",10)>
<?php
$ip = getenv ("REMOTE_ADDR");
$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$combine = $ip . " tried to load " ;
$url = $servname . $requri ;
$httpref = getenv ("HTTP_REFERER");
$httpagent = getenv ("HTTP_USER_AGENT");
$today = date("D M j Y g:i:s a T");
$message2 = "On $today, $combine:\n
http://www.$url\n
User Agent = $httpagent \n
$httpref ";
$to = "email@yourdomain.com";
$subject = "404 Not Found Error Report";
$from = "From:email@yourdomain.com\r\n";
mail($to, $subject, $message2, $from);
?>
</body>
</html>

Change 'redirecteddomain.com' to the domain with the 404 not found error landing page, and change 'email@yourdomain.com' to your email address. Now save the file as '404.php'. Upload this file to all your web sites.

Now when a user encounters an error on your site, the above file sends you a 404 not found error email notification. The user is then redirected to the 404 not found error landing page (below) on your main domain.

404 Not Found Error Landing Page

Now create another blank file and add the following code:

<html>
<head><title>404 Not Found Error</title>
</head><body>
<table border="0px" cellspacing="0px" cellpadding="0px" width="100%"><tr><td bgcolor="#FFFFFF" valign="top" width="55%">
<h1>Sorry!</h1>
<table width="60%" align="center"><tr><td>Sorry but we couldn't find what you're looking for! Don't panic - it's not your fault. We have been notified of the error.</p>
<p>Here are a few options to find what you're looking for:</p>
<ul>
<li>Double check the web address for typos
<li>Click the 'BACK' button in your web browser
<li>Search the site using the search form above
<li>Go to the <a href="http://www.yourdomain.com">homepage</a> (replace yourdomain.com with your website's homepage)
</td></tr></table>
</body>
</html>

Save this file as '404.html'. Note that the file extension is '.html' as opposed to '.php'. Upload this file to the domain to where you want the 404 not found error landing page to be located.

Don't forget to upload the 404 not found error email notification 404.php file to your main domain as well. So you should have both the 404.php (email notification file) and 404.html (landing page) on the main domain server.

You can view my 404 not found error webpage at:

http://www.mikes-marketing-tools.com/404.html

So there you have it. Set up your very own real-time 404 not found error email notification system and you will be notified the instant an error occurs on your websites.

A Word of Warning

You may experience a flood of 404 not found error email messages. I received 196 error messages in 24 hours, although many of them were duplicates which were sent for the same error until I fixed the relevant issue.

Don't worry. Simply go through each email and fix each issue you find. Once you fix all the issues, the flood of emails should slow to a trickle. If you get annoyed with all the error messages, simply remove the ErrorDocument code from your .htaccess file.

I hope you found this article useful. I am sure there are other, probably better, ways of setting up a real-time 404 not found error email notification system, but that is beyond the scope of this article and my technical knowledge. ;-)

Disclaimer

Please note that the PHP/HTML code in this article may not work on all servers. If you use the suggested code, you understand and accept any advice you follow in this article is done so at your own risk, and you do not hold me liable for any damage done. If you are concerned at all, then please ask your website administrator to help you.

About the author: Michael Wong is the editor of Mikes-Marketing-Tools.com and author of MichaelWongAcademy.org, which shows people how to make money online. Mike entered the internet industry in 1998. He sold a website to a SoftBank funded start-up in 2000. He wrote one of the earliest SEO books in 2002. And he's generated millions in online revenue since then.
You have Mike's permission to republish this article in your website, on the condition you include Mike's bio after the article.