How To Create a 404 Error Handler For Your .NET Application
I've always liked custom 404 error handlers. I've never liked to give a user a 404 - Page Not Found Error. One of my very earliest websites included a 404 handler that tried to intelligently redirect the user to something related to the wrong URL they'd tried to jump to.
The best 404 error handlers are probably dynamic scripts that try to supply context sensitive information to the user. But handling mistyped urls is only one use of a 404 error handler.
Another improtant use for error 404 handlers is to put friendly urls on top of script generated output. Until recently, some search engines were unable to spider script type urls with parameters. When an error404 handler is used to return web pages for normal URLs, this can make content more easily digested by various web spiders.
Many wikis have URLs that look like this: http://www.chat11.com/cgi-bin/wiki.pl?Programming
By having the wiki program respond as a 404 handler, it is possible to use much simplier URLs such as http://www.chat11.com/ProgrammingSetting Up The 404 Error Handler
- Configuring Error 404 Handler For Apache
It is easy to configure an Apache webserver to use a 404 error handler. It can be done as simply as including a single line in an .htaccess in an http file directory, such as the root directory of a virtual web host:
ErrorDocument?Create 404 /cgi-bin/wiki.pl
- Configuring Error 404 Handler For IIS
On Microsoft IIS web servers, use the Internet Information Services (IIS6) Manager to configure a 404 handler for any directory, virtual root, or entire website.
Right click on any folder, select Properties. Select the Custom Errors tab. From the list of errors, select HTTP Error 404. Then click on "Edit...". Select message type of URL, then enter path and file of the error 404 handler. With .NET, your error handler page might be named Error404.aspx, so you might enter /Error404?Create.aspx for the file in the root directory. If you are setting up the 404 error handler just for your own application subdirectory, you might enter /YourApp?Create/Error404?Create.aspxProgramming The Error 404 Handler
One of the first things you will want to do is write a small program to show you all the server variables that are available to you from within the error handler script.
Here is some sample C Sharp (C#) to return a string containing all the server variables. Put this code in your Error404.aspx.cs file:
It's very simple to put this into your page. Put the following line into your Error404.aspx page:
The most interesting variable will be the QUERY_STRING variable:
It is easy to pick this apart, and create a new URI object to use with the URL that generated the 404 error:
Other related issues:
- How to setup a 404 handler that works with the development server that comes with Visual Studio 2005. Because projects under that environment usualy include a path, it is sometimes difficult to deal with applications that depend on 404 handling, since there isn't a console for the development server to set a 404 handler. Whereas an application might see the URL /home in its 404 handling routines, when debugged from the development server, it may see something like http://localhost:9384/AppName/home
- Google search - "ASP development server" 404 handler - No results
- Google search - "ASP.net development server" 404 handler