Tracking 404 Errors
By Nathan Pond
"404 - File Not Found" We've all seen this hundreds of times. As hard as we all try to spare our users from
seeing such a thing, the simple fact is there is no way to prevent this from happening. However, configuring
IIS 4.0 and using some simple ASP can display a custom, formatted page to the user while at the same time
logging the incorrect URL to a database or sending the webmaster an e-mail. You can not afford to wait for
your users to report incorrect links on your site, you want to be notified when the error happens.
In this article I'll explain how you can do that.
Keep in mind that you don't have to log errors, you may simply just want to use your own 404 page instead of
the default one that shows on the users browser. In either case, the first step is to create the custom 404
page that you want to show to the user. You can be as creative as you want with this, some web sites have
very humorous 404 pages. You can visit http://www.npond.com/404.shtml
to see what I did with mine, maybe give you some ideas.
When creating the page, you will probably want to name it /404.asp for easy recognition. If you
do not want to track the error to a database or e-mail, you could make it a plain html file instead of ASP.
|
You have your page created, at least the html part of it. We'll get the ASP in a little bit. The next step is
to configure IIS to send your 404 file whenever it can't find the file specified by the users browser. To do this
open Microsoft Management Console (MMC). You may make the 404 page you have work for your entire server, only
specified webs, or even specified directories within a web. For now we will configure one web on your web
server to use this 404 page. Right click on the name of the web you wish to configure, and click on
Properties. A dialog window opens with many different tabs, you want to click on the
Custom Errors tab. Here you have a list of every server error IIS has a page for. You can
customize anyone of them, but for now scroll down the list and select 404, and click on the
Edit Properties button.
Now a dialog window pops up and gives you the option to select the Message Type; if you are
simply displaying a formatted page and aren't using an ASP file, you can select File from this
list. If you are using an ASP page you should select URL. If you selected File as
the message type, you should then click the Browse button under the text box to select the file
to use. If you selected URL you must type the virtual path (/404.asp) on your
server to the script that will be executed.
Click the Ok button to close this dialog, then click Ok to save the web properties.
At this point, your web will now show your 404 page instead of the default 404 page. Now it's time to put
some ASP code into it!
When a 404 error is triggered, IIS will direct the user to your custom ASP script and pass the incorrect URL
in the querystring. All we need to do is parse it. We can also use Server Variable HTTP_REFERER
to find out if this page was linked to, and where it was linked from. Put the following ASP code near the
top of your /404.asp page:
<%
Dim strURL, strReferer
strURL = Request.ServerVariables("HTTP_URL")
strURL = Right(strURL, (Len(strURL) - (Instr(strURL, chr(59)))))
strReferer = Request.ServerVariables("http_referer")
%>
|
This code will assign the strURL variable with the incorrect URL that the user tried to access. It will also
put the refering page into the strReferer variable. Now it is a simple matter of sending an e-mail using
CDONTS (or some other component) and/or loggin this information to a database. Note that if strReferer
is empty or null then the page was not accessed via a link, the user typed in the address. If
strReferer has a value it is the value of the URL that linked to the non-existent file on our
server.
For information on using CDONTS to send e-mail from ASP, please see:
Sending Emails in ASP using CDONTS.
|