CoverYourASP --> Trapping HTTP 404 - File not found

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
135 active users
4534 visitors today
3406 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2016 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

Everyone has seen an HTTP 404 error at some point. You follow a link on a web page, and BOOM - File not found. Perhaps the author spelt it incorrectly, or the page was uploaded to the server by mistake.

I'll show you how to handle this gracefully*, and send an email to yourself so the problem gets fixed. But first, what did I mean when I said "Make money from error handling"?

I was authoring a site last year that wanted to offer customers their own websites. While thinking about the URL's I should give them I got to thinking about Bigfoot. I have long had Bigfoot redirect traffic to this site from a generic "http://www.Bigfoot.com/~jamesandtara" URL that's been around for years.

All of a sudden I had one of those Eureka moments! I'd just give my customers a URL such as "http://CoverYourASP.com/~Yourname". This is already a common form of URL found on many sites who have "members pages".

The next step was easy. I wrote a page that would handle the 404 File not found errors, and checked if they started with a '~' character. Any that did I looked up in my database and served up their page from that data. Hey presto, unlimited website URL's for everyone!

To demonstrate the principle, here are 3 URL's that don't exist on this site. Click on them to see the results.

• NoFile.asp - this file doesn't exist. Apologize to the user, and send myself a mail to fix the problem using CDONTS.
• ~James - because this missing "file" starts with a ~, and doesn't contain an extension, I treat this as a customer ID. (James is in fact the only "known" customer ID.)
• ~WhoKnows - demonstrates how you could respond to an unknown customer ID.

UPDATE: I had an interesting experience last night. My web site started sending emails as fast as it could informing me of missing files. I got over 500 before I FTP'd onto the server and deleted the Handle404.asp file.

The reason was a "spider" crawling my page - it had got onto this page and was trying to get to the ~James page shown above. Unfortunately it had interpreted the lack of file extension as a folder name and had helpfully appended a trailing /. Things went downhill from there because all my hyperlinks from that page are relative, not absolute.

Do you see where this is leading? Because the spider had asked for a new folder called ~James my relative links were all broken, and generated 404's themselves! I hate to think how many emails I would have got if I was on vacation... The moral of the story: Always use absolute paths in the 404 handling page, or use a <base href="http://youdomain.com/"> tag to keep all your URL's relative to that rather than the current file.

* I don't need to describe how to setup IIS to use custom error handling - it's fully described in the IIS help - just browse to your local IISHELP documentation.

Featured sponsor
My favorite resources


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...