When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Wednesday, October 17, 2001

Creating Pseudo Forwarding Areas - the Easy Way!

By Joe Bennett

Unless you spent considerable time on the planning of your Web site's physical file structure, chances are you have important areas of the site buried in deep URLs. For example, perhaps a main section on your Web site is a job board type section, which may be located at: http://www.yourserver.com/ASPScripts/forums/jobsHome.asp. Wouldn't it be nice if you could provide an alternate URL, like: http://www.yourserver.com/jobs/? Well, you can, quite easily, and in this article, I'll look at how to accomplish this technique without having to create actual directory entries in your Web site!

- continued -

Furthermore, this technique can be used if you run a site with user accounts where each user has a customized section to provide customized URLs like: http://www.yourserver.com/Username/ that could redirect to, say: http://www.yourserver.com/users/UserInfo.asp?Username=Username.

Getting Started
The first thing that needs to be done to implement this technique is to build a custom 404-error page. (If you are unfamiliar with how to build a custom 404 error page, be sure to read: Creating a Custom 404 Error Page.) Name this custom 404 error page anything you want, but make sure that it is an ASP file (in my example I will use 404.asp). Understand that the HTTP 404 error occurs when the user requests a page that does not exist. This custom 404 error page can do anything a regular ASP page could do. So, when a user visits one of our "customized," nonexistent pages, like http://www.yourserver.com/jobs/, we'll have the 404 error page issue a Response.Redirect to http://www.yourserver.com/ASPScripts/forums/jobsHome.asp!

Custom 404 Error Pages on a Hosted Account...
If you are using a hosted account you will need to contact the host and have them point the 404-error to your specified ASP 404 error handling page. For my personal account, they pointed to my 404-error page in an hour, for free. Your mileage may vary, though.

Writing our Custom 404 Error Page
Now that the server is pointing to our 404.asp file it's time to add some script to make it all work! Realize that when the user enters an invalid URL, he is redirected to our specified custom 404 error page along with the URL the user attempted to enter. This important information is appended to the QueryString in the form: 404;complete URL entered by user. Therefore, in our custom 404 error page we need to first grab the QueryString to find out where the user needs to be sent. We can grab the URL the user attempted to enter with the following statement:

strRedirect = Mid(lcase(Request.Querystring), 5)

This line reads the 5th through last characters in Request.QueryString into the variable strRedirect, which, essentially, grabs the complete URL entered by the user. Specifically, we are using the Mid function to chop off 404; from the QueryString and converting everything to lowercase.

Now that we know the URL the user entered, we can redirect them to the proper page. There are a number of ways to do this. One is to hard code in the redirect values, like so:

Select Case strRedirect
  Case "http://www.axdo.net/4guys"  
    Response.Redirect "/4guysfromrolla/article/foo.html" 
  Case "http://www.axdo.net/media"
    Response.Redirect "/projects/mediacenter/dynamicSearch/index.asp"
  Case "http://www.axdo.net/users"
    Response.Redirect "/users.asp"
  Case "http://www.axdo.net/Username"
	Username = Mid(Request.QueryString, InStrRev(Request.QueryString, "/") + 1)
    Response.Redirect "/users.asp?Username=" & Username
  Case Else
    ... code for your normal 404-error page, or redirect ...
End Select

Notice that you can redirect users to any type of file (HTML, ASP, TXT, etc.). In the above example you can see that if a user entered http://www.axdo.net/4guys into their browser's location window they would be directed to /4guysfromrolla/article/foo.html. Likewise, to reach the media center search, users would only need to enter: http://www.axdo.net/media. Also note how in the http://www.axdo.net/Username example, the username entered after the last forward slash is read into the variable Username and then appended as the QueryString value in the subsequent Response.Redirect line. Finally, notice that you should add a redirect for a regular 404-error page under the Case Else statement, in case the user does enter a nonvalid URL.

Another way to store the redirect values is in a database. A simple database table with two columns: PseudoUrl and RealUrl could be constructed. Then, when a user hit the 404 error page, a quick lookup could be performed to see if the user's entered URL was found in a PseudoUrl column in the table; if it was, the user could be automatically redirected to the value in RealUrl. This technique would be easier to manage, especially if you planned on having a large number of pseudo URLs that could be redirected to real ones.

Another Application...
Another use of this technique could be used to predict common misspellings in order to proactively reduce the number of 404 errors. What I mean by that someone confounded statement, is that if your site had a URL that was, say: http://www.yourserver.com/Tutorials, you could add entries to catch common spelling mistakes, like: http://www.yourserver.com/Tutorial, http://www.yourserver.com/Tutorilas, and http://www.yourserver.com/Tutorialss. All of these would then just redirect to: http://www.yourserver.com/Tutorials.

The technique presented in this article illustrates how to create several pseudo forwarding sections on your Web site. Creating such a forwarding system doesn't require that much work - just a custom 404 error page! Furthermore, these forwarding areas can be used to create customized direct links for important areas on your site, for users expecting customized content, or for catching commonly misspelled words to proactively reduce the number of 404 errors. I hope you find this technique useful and decide to implement it in your site!

Happy Programming!

  • By Joe Bennett

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article