Important alert: (current site time 7/16/2013 EDT)
 

article

Tracking 404 Errors

Email
Submitted on: 7/31/2000 11:11:25 AM
By: Nathan Pond  
Level: Beginner
User Rating: By 1 Users
Compatibility: ASP (Active Server Pages)
Views: 16891
(About the author)
 
     This will explain (step by step) how to customize your 404 File Not Found page in IIS 4.0. It will also tell you how to use ASP to log this information so that it may be corrected, you could log it to a database, send an email to the webmaster, or just about anything you want to send out an alert.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
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.


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
8/21/2000 7:57:54 AMLuis Ferro

I just want to notice that if the users have
(If this comment was disrespectful, please report it.)

 
2/28/2001 5:22:02 PMDedw8

I know little about asp programming, but to me it looks like you DIM statement does not correctly declare your variables. Small, but the statement should read:

Dim strURL, strReferer.

Am I out to lunch?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.