That boring 404 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)?
Using a feature of IIS 4 and IIS 5.0, 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) & "," end if Response.Write "Sorry, the URL you were looking for" & msg & " is not available." %> |
Keep in mind that once you hit the 404 ASP page, you no longer have access to any anchor or querystring information they originally asked for.
There are many other things you could do, such as send an automated e-mail or append a log file every time someone hits a 404. After collecting data on non-existent pages that are queried often, you could anticipate them by checking the value of
b above and suggesting where they should go instead.
For a more in-depth article on building a 404-centric application, see the following:
http://msdn.microsoft.com/library/en-us/... Note that .NET Server, at least in the few builds following Beta 3, does not support URL for custom 404 error pages. Still waiting on word about why this was left out, whether it will return before IIS 6.0 is released, and if not, what it will mean for users upgrading and expecting to continue using URLs for this purpose.