Give them a FRIENDLY 404 error
by Aaron Bertrand
That grey, boring error message leaves a lot to be desired. Wouldn't it be nice to be able to log those errors, find out where the bad links are coming from, and most importantly, inform the user that the URL they entered is incorrect (and, where applicable, suggest to them where they should have gone)? That grey, boring error message leaves a lot to be desired. Wouldn't it be nice to be able to log those errors, find out where the bad links are coming from, and most importantly, inform the user that the URL they entered is incorrect?
Using a rarely-documented feature of IIS 4 and IIS 5, you can do all of these things and more. What you want to do is create a page in your site called 404.asp. This is the page that will be presented instead of that stock grey page, so you'll likely want to make this page look like the rest of your site. Here is an example. One important thing to note is that this page should reference all images and links relative to your root folder, as the 404.asp file will actually execute in whichever folder it's called from. For example, if you have <img src="image.gif"> and that image only exists in your root folder, then if someone calls /folder/no-exist.htm, the image will show up as a broken link because it does not exist in /folder/ ... therefore you should always use <img src="/image.gif"> or <img src="/images/image.gif">. Same goes for links, they should be relative to the root URL, not to the folder that 404.asp resides in.
Once you have built a standard 404 page, you can make IIS intercept 404 errors and present this page instead by following these steps:
- Open Internet Services Manager
- Expand the trees until you see Default Web Site
- Right-click Default Web Site and choose Properties
- Highlight the Custom Errors tab
- Scroll down to 404 and click Edit Properties...
- Choose URL from the Message Type listbox
- In the URL textbox, type the relative path
of your 404 page
(Starting with the slash (/) that indicates your root)
You may want to add a bit of flexibility to this code, for example you can display the page they were trying to reach by accessing the querystring:
- b = request.servervariables("query_string")
msg = " "
if len(b) > 0 then
' Take out "404;" from querystring
msg = ", " & right(b,len(b)-4) & ", "
Sorry, the URL you were looking
for<%= msg %>is not available.
There are many other things you could do, for example send an automated e-mail or append a log file every time someone hits a 404. After collecting data on non-existant pages that are queried often, you could anticipate them by checking the value of b above and suggesting where they should go instead. For example, if you hit http://www.desktop.on.ca/tip/, you will be pointed to /tips/ ... I simply search for the word "tip" in a 404 string, and if it's there, I know that they were trying to find my /tips/ section but had a typo in the URL.