ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 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
331 active users
1718 visitors today
2565 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

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

ASP.NET Blog
ASPRSS
ShawThing

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

Selling products online - read how James does it now



Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


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...