Keith's Software and Tutorials Home Page
Knowledge Is Power

Bad Referral Link
Auto Notification
Free CGI Script







Companion pages:


How To Use Your CGI-BIN

About htaccess & XBitHack

Trying Sun Solaris For Intel x86

Automate Cisco Device Monitoring

CGI Scripts On Windows NT / IIS

Windows PPTP Clients with Cisco VPN Routers






On the htaccess and XBitHack page we show you how to set up a professional looking 404 (page not found) error page that matches your site's appearance so that it gets displayed instead of the generic 404 error page generated by most Web servers.

What if the reason visitors were getting this page was because someone else has a bad link to your Web site or page (a misspelled URL or a link to a page that you have removed)? Wouldn't it be nice to find out which referring pages have bad links to your site or pages? That's what this CGI script does. When someone encounters a 404 error on your site it will automatically send you an e-mail with the specifics (URL of the offending page, client info, etc.) so that you can contact the Webmaster of the referring site to have the link corrected. If you're not familier with how to use CGI scripts, our How To Use Your CGI-BIN page will show you how to do that.

You'll need to create an error HTML page and set up a .htaccess file to call the page in the event of a 404 error, but we show you how to do that on the htaccess and XBitHack page. Once that's done, all you have to do is set up this CGI script and add an SSI directive tag to the error HTML page to call the script.

Note:  This script was written for UNIX/Linux servers. If you know how to modify CGI scripts to work on Windows servers you should be able to get it work with IIS. See the CGI On Windows IIS page for some help with this. However, you will also need to know how to specify a non-generic 404 page in IIS. Consult the IIS documentation for this.
Once everything is set up, each time the error HTML page is called by the .htaccess file and displayed, the script gets executed via the SSI directive. The script checks to see if the referral information is blank. Blank referrer information indicates that the visitor simply typed a bad URL directly into the location line in their browser. In such a case the script does nothing. However, if there is referrer information in the server's environment it indicates that the visitor got the error page by clicking on a bad link on someone else's site. In this case the script will e-mail you an alert similar to the following:


To: you@yourisp.com
From: 404-ALERT@yourhost.com
Subject: Referrer 404 Alert for www.yourwebsite.com

A visitor from:
  cache-rf06.proxy.aol.com  (152.163.188.166)
using the following OS and browser:
  Mozilla/4.0 (compatible; MSIE 5.0; AOL 8.0; Windows 98
was forwarded on to the error page on the
  www.yourwebsite.com Web site.

The bad link they clicked on that caused them
to be forwarded to the error page is at:
  http://www.theothersite.com/theirlinkpage.html

You should contact this site's Webmaster and
ask them to correct their link.


This is a fairly simple script. You only need to enter three values:



If you're familier with CGI scripts, setting up the script is easy.

THAT'S IT!!! The only problem is, if no one has any bad links to your site or page you'll never know if the script works or not. You may want to set up a test HTML page with a bad link to test it. Just create an HTML page with a link to your home page, but misspell the page name like so:

<a href="http://www.yourwebsite.com/indxe.html">

If you get errors displayed on the error HTML page (or when the error HTML page is supposed to be displayed), check out the Troubleshooting page for our Bestdam Logger Visitor Logger and Hit Counter script for Websites. It gives the most common types of CGI errors and what to check when they're encountered, plus some troubleshooting techniques. WE DO NOT OFFER SUPPORT FOR THIS SCRIPT so please don't e-mail us if you have problems using it.

If someone goes to a page that does exist, the server will still generate 404 errors if something referenced on that page does not exist. For example, if have an <img src> HTML tag that references a graphics file that is missing, a 404 error will be generated by the server but not displayed on the page. In these cases the "bad link they clicked on" will be a page on your site. As a matter of fact, a page with numerous such missing references will generate numerous e-mail notifications (one for each missing item). In this capacity, the script serves to alert you missing items or incorrect references. Viewing the page in a browser may indicate what is missing.

You may occasionally get e-mails that don't make any sense. For example, the "bad link they clicked on" will be a .gif file on your site. This is due to the Web server software (usually Apache or IIS) having some bogus info in the referrer field. Unless the same error is received on a routine basis, these alerts can be disregarded.

Here's the script code. Just drag you mouse over it to highlight it and copy it to your clipboard.


#!/usr/bin/perl

print "Content-type: text/html\n\n";     # Display errors on the page

############################################################################
#
#     WhosWrong CGI Script by Keith Parkansky - www.parkansky.com
#     Perl script - SSI required
#     Released as freeware - January 2003
#     PROVIDED WITH NO WARRANTIES OR SUPPORT OF ANY KIND
#     Use of this script constitutes acceptance of
#     all risks and responsibilities for consequences
#     of the use of this script.
#
#     What This Script Does
#     ---------------------
#     If another Website has a link to a non-existent page on
#     YOUR site, the visitor will generate a 404 error when they
#     click on this bogus link on the other site.
#     This script will e-mail you an alert containing the URL of
#     the page on the other site so that you can contact its'
#     Webmaster so that you can ask them to correct their link.
#     No alert will be e-mailed if there is no 'referrer' info,
#     as would be the case if the visitor simply mistyped the URL
#     to one of your site's pages in their browser's location line.
#     
#
#     THIS SCRIPT WAS WRITTEN FOR UNIX/LINUX SERVERS!
#
#
#     How To Use It
#     -------------
#
#      1. Create some sort of 'error.html' page that gets displayed
#         in the event of a 404 (Page not found) error.
#              This requires the use of a '.htaccess' file on UNIX
#              and Linux servers.  Details on how to set up a
#              '.htaccess' file are given at:
#              https://www.parkansky.com/bdl/bdlogxbh.htm
#
#      2. In this file:  enter the correct Perl path on your server
#         (shebang line) in first line of this script (top of this file).
#
#      3. In this file:  enter your values for the two user variables
#         immediately following this comment block.
#
#      4. Add this SSI directive tag to the bottom of your error.html
#         page somewhere before the closing </body> tag and ftp the
#         updated error page to the HTML directory on your server:
#
#            <!--#exec cgi="/cgi-bin/whoswrong.cgi" -->
#
#
#      5. USING  ASCII  MODE !, ftp the  whoswrong.cgi  file to the
#         cgi-bin directory on your server.
#
#      6. CHMOD the file 755
#
#
#      IT IS BEST TO VIEW THIS FILE WITH 'WORD WRAP' TURNED ***OFF***
#
############################################################################


  #  YOU ***MUST*** SET THE  2  USER VARIABLES FOR E-MAIL ADDRESS OF
  #  OF THE RECIPIENT OF THE ALERTS AND THE PATH TO THE E-MAIL PROGRAM
  #  ON YOUR SERVER !

  #      Example:   $sendto = 'bozo@circus.com';
  #                 $mailprgm = '/usr/lib/sendmail -t';


$sendto = 'you@yourisp.com';

$mailprgm = '/usr/bin/sendmail -t';


#==========  STOP HERE !  -  Save your changes and exit
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#


   #--- Environment (visitor and page info)
$sitename = $ENV{'SERVER_NAME'};
$client = $ENV{'HTTP_USER_AGENT'};
$camefrom = $ENV{'HTTP_REFERER'};
$addrin = $ENV{'REMOTE_ADDR'};
@ip = split (/\./, $addrin);
$binip = pack ("C4", @ip);
$isp = gethostbyaddr ($binip, 2);

#========================  Start of MAIN routine  ====================


   #--- If there is referrer information

if ($camefrom ne '')   
{

      #--- Open the user-specified e-mail program with recipient info

   open (MAIL, "|$mailprgm") || die 'Could not open mail program';
   print MAIL "To: $sendto\n";
   print MAIL "From: 404-ALERT\n";
   print MAIL "Subject: Referrer 404 Alert for $sitename\n\n";

      #--- Print the contents of the e-mail message and send

   print MAIL "\n";
   print MAIL "A visitor from:\n";
   print MAIL "  $isp ($addrin)\n";
   print MAIL "using the following OS and browser:\n";
   print MAIL "  $client\n";
   print MAIL "was forwarded on to the error page on the\n";
   print MAIL "  $sitename Web site.\n\n";
   print MAIL "The bad link they clicked on that caused them\n";
   print MAIL "to be forwarded to the error page is at:\n";
   print MAIL "  $camefrom\n\n";
   print MAIL "You should contact this site's Webmaster and\n";
   print MAIL "ask them to correct their link.\n\n";
   print MAIL "Alert sent by WhosWrong free CGI script:\n";
   print MAIL "  https://www.parkansky.com/tutorials/404alert.htm\n\n";
   close (MAIL);

}  

exit;



Top of page





Contents, diagrams, and images    Copyright © 2004-2023    Keith Parkansky    All rights reserved.
Certain graphics, symbols, and terms used on this site and in its documents are registered trademarks
of their respective owners and are contained herein for identification purposes only.
No endorsement of this site, its contents, or its documents by these owners is expressed or implied.

LIABILITY
IN NO EVENT WILL KEITH PARKANSKY BE LIABLE TO ANY PARTY (i) FOR ANY DIRECT, INDIRECT, SPECIAL, PUNITIVE OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR INFORMATION, AND THE LIKE), OR ANY OTHER DAMAGES ARISING IN ANY WAY OUT OF THE AVAILABILITY, USE, RELIANCE ON, OR INABILITY TO USE THE INFORMATION, METHODS, HTML OR COMPUTER CODE, OR "KNOWLEDGE" PROVIDED ON OR THROUGH THIS WEBSITE OR ANY OF ITS' ASSOCIATED DOCUMENTS, DIAGRAMS, IMAGES, REPRODUCTIONS, COMPUTER EXECUTED CODE, OR ELECTRONICALLY STORED OR TRANSMITTED FILES OR GENERATED COMMUNICATIONS OR DATA EVEN IF KEITH PARKANSKY SHALL HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, AND REGARDLESS OF THE FORM OF ACTION, WHETHER IN CONTRACT, TORT, OR OTHERWISE; OR (ii) FOR ANY CLAIM ATTRIBUTABLE TO ERRORS, OMISSIONS, OR OTHER INACCURACIES IN, OR DESTRUCTIVE PROPERTIES OF ANY INFORMATION, METHODS, HTML OR COMPUTER CODE, OR "KNOWLEDGE" PROVIDED ON OR THROUGH THIS WEBSITE OR ANY OF ITS' ASSOCIATED DOCUMENTS, DIAGRAMS, IMAGES, REPRODUCTIONS, COMPUTER EXECUTED CODE, OR ELECTRONICALLY STORED, TRANSMITTED, OR GENERATED FILES, COMMUNICATIONS, OR DATA. USE OF THIS SITE CONSTITUTES ACCEPTANCE OF ALL STATED TERMS AND CONDITIONS.