Creating "Quick-Links" Using a Custom 404 PageBy Johan Alveborg
The other day one of the marketing guys came around and asked if we
couldnít fix up some easy-to-type URLís for some pages on the site.
The site uses frames and the page requested should of course show up
in the main content frame in the same way as if the user had chosen it
from the menu. Until now, we had solved this with a parameter to the
frame-definition page, such as
then checking the
Page-param in a
statement for the correct page to display.
Now, the marketing departement had dreamed up about 35 new direct-links
they wanted implemented, and they were of the opinion that
http://www.foo.com/somepage?page=42 didnít really look
very good in print ads, and I had to agree :-)
The two most obvious ways to solve this were:
1.) Add 35 new virtual roots to our 25 IIS boxes, or
2.) Add 35 new directories in the webroot, each with a default page that does a
SomePage.aspwith the correct parameter.
On of our hired hands started off along the lines of option #2, but
I started thinking there hade to be a better way, and fortunately there
is! What happens if you request
the directory doesnít exist? You get a "404 Page not found"-message.
Hrmmm, let's check... yes you can configure what page IIS serves when
the requested file or directory doesnít exist, but it doesnít seem to run
the ASP-code put in the page. What eluded me at first was that in the
IIS MMC you can set what type of page IIS should use for the error
messages, the default is "File" which just shoots the page to the user
without any processing. Changing the type to "URL" does the trick and
any ASP code in the page is now processed. IIS sends the URL the user
requested in the query string so itís available to our ASP code to
|For More Information on Custom 404 Error Pages|
|To learn more about creating custom 404 error pages be sure to read Creating a Custom 404 Error Page. It is a very good and thorough article explaining how to setup such a custom 404 error page! Another very worthwhile read on the subject is Nathan Pond's Tracking 404 Errors.|
So, the rest is pretty straightforward: In the ASP page IIS serves when it canít find a requested folder or file, check what folder the user requests and if itís one that you have anticipated, you redirect the user to the correct page Ė in our case the frame definition page with a parameter indicating what page to display in the main content frame.
Begin by creating a
404.asp page and configure IIS to
serve that page for the 404 error code (donít forget to set "Type" to
"URL"; for more information on setting up IIS properly and creating
such a custom 404 error page, be sure to read Creating a Custom 404 Error Page!)
404.asp, snarf out the directory the user has tried to
access using the following code:
and then use a
Select Case to check and redirect as needed:
You could of course tidy things up by using an array of names/numbers
(in the format
BOOKS;25) stored in an application variable
and loop through it. This would allow for an easy web-based updating
mechanism for adding new pages without having to change the
404.asp page. To accomplish this, in
(or an admin page) set the array up as:
And in the
Don't use the dictionary object (which otherwise would have been the
perfect method) since it's not thread safe. The
LookupTable object looks good but I havenít tried it yet.
One of the coolest things about this hack is that it's very easy to
handle user requests for misspelled URL's: In the above example, just
BOOK to the array and you'll
at least have increased the chances that the user will get to the
page that they intended!