Sample Code

Send Email for HTTP Error Codes 401, 403, 404, 500 using Perl CGI-bin

Ever wondered how long one of your websites has been down, but you didn't check your error_log file? There is an easy way to be alerted when there is an error on your website, such as 404 Not Found, 500 Internal Server Error, and others. I have made a simple CGI script using Perl that sends an email to the webmaster saying what page caused the error, what the error code is, and what the visitor's IP address is. I call it

You can download here or copy and the code below and paste it in your own file.

Take your copy of and upload it to your cgi-bin directory. Use FTP, SSH, or anything similar to change it to an executable file. For example, in SSH you can type "chmod +x" without the quotes. It sends an email and then forwards to the error document you specify. The default is error.html.

Once this is taken care of, modify .htaccess in your HTTP root directory (public_html), or create it if it does not exist. You can download a sample of .htaccess here. Be sure to save it as "All Files" for it to work properly. Inside .htaccess, place the following lines in it:

ErrorDocument 401 /cgi-bin/
ErrorDocument 403 /cgi-bin/
ErrorDocument 404 /cgi-bin/
ErrorDocument 500 /cgi-bin/

You can use whatever error codes you choose, but these are what I am using in this example. Using these codes, all Unauthorized, Forbidden, Not Found, and Internal Server Error requests will execute the script.

Be sure to have your error.html (or similar) document on your server because the script will forward to it after the script sends the email. You can configure the error document's name in the Perl script, as well as the webmaster's email address. To see my error.html, click here.

To review, you should have 3 files on your web server: cgi-bin/, .htaccess, and error.html. All of these files work together to produce the webmaster's alert and keep the visitors from seeing the default error documents.

If you have questions or comments about this script, please use the live chat feature on the website here or send an email to the address on the contact page. This is not the only way to alert the webmaster, but it works well. Feel free to recommend revisions.