|
|
| Databases 101 | | | ASP.NET Chapter | | | Suggest Article | | | Discussions | | | Bookstore | | | Write for Us | | | OpenAuction | | | Our Authors | |
|
|
|
Internet Jobs
E-Business Exchange
International Domains
Add a Career Center
Buy Computer Products
Internet Intelligence
IT Solutions
Training Solutions
Add Online Sales Reps
Outsource IT Work
|
 |
|
| Creating Aliases to URL's | | by Pete Nelson | | | Skill level: Intermediate | | | First posted: Wednesday, November 10, 1999 | |  | |  | | Overview
On the web site I run, DVD Tracker, people can keep an online catalog of their DVD collection and can share it with other users. After running the site for a few months, I often found that people would post links to their list on other pages.
However, because we link to an ASP page and pass their user ID in the query string, these links would be long, confusing and not easy for users to remember. Besides, seeing something like http://www.dvdtracker.com/dvd_list.asp?user_id=pete@dvdtracker.com in a Usenet post is just plain ugly.
Wouldn't it be nice if people could link to a list with a nice-looking, easy-to-remember URL like http://www.dvdtracker.com/~weasel ?
A Good First Attempt
Our first solution was to create a separate .ASP file for each user. In our user table, we had a field that kept track of the alias they were using. This was used to prevent other people from using the same alias and overwriting their file. We then had a function that would write out ASP code to their file. Thus, a link like http://www.dvdtracker.com/~weasel.asp would run the ~weasel.asp file. Contents below:
<% Response.Redirect "dvd_list.asp?user_id=pete@dvdtracker.com" %>
|
The problem with this is that after a while, we had several thousand .ASP files cluttering out root directory! Worked great for the users, terrible for us administrators.
A Better Solution (or How 404s are Your Friend)
The reason we had originally gone with .ASP files is that IIS would return a 404 Not Found if we had just done something like http://www.dvdtracker.com/~weasel. However, IIS has a great thing called Custom Errors. You can tell IIS to run an ASP page if any sort of error has occured, including 404s! By passing the whole URL to the 404, we are able to capture the alias and redirect to the proper page when an alias link is used.
The first thing is to set up IIS to go to a URL when a 404 error occurs. Start the IIS Manager, go to the Properties on the web site, click on the Custom Errors tab and Edit the Properties on the HTTP 404 error. Set the Message Type to URL and the URL to whatever your file will be. We used /404.asp.
Now that IIS is set up correctly, you can create your 404.asp file to process the aliases. By using the Query_String server variable, we're able to see what URL the browser was looking for. If we see a tilde "~", then it's probably an alias and we query the database to find the user ID. If it exists, we redirect to the proper page.
<% strTarget = Request.ServerVariables("QUERY_STRING")
' *** Possible alias reference if instr(strTarget, "~") then
' *** Remove the ~ and any .ASP from the alias strAlias = trim(right(strTarget, len(strTarget) - instr(strTarget, "~"))) strAlias = LCase(replace(UCase(strAlias), ".ASP", "")) if trim(strAlias) <> "" then ' *** Query the users table to see if this alias belongs to anyone Set recTemp = Server.CreateObject("ADODB.RecordSet") recTemp.Source = "select user_id, list_type from users where alias_file = '" & Replace(strAlias, "'", "''") & "'") recTemp.ActiveConnection = conDB recTemp.Open
' *** Found user record, redirect to their list if not (recTemp.BOF and recTemp.EOF) then strRedir = "dvd_list.asp?user_id=" & Server.URLEncode(trim(recTemp("User_ID"))) recTemp.Close Set recTemp = Nothing Response.Redirect strRedir
end if recTemp.Close Set recTemp = Nothing end if end if
' *** No alias link was found. Write 404 status to the client and display error message
Response.Status = "404 Not Found"
%>
<html> <head><title>404 Not Found</title></head> <body>
404 Not Found
</body> </html>
|
Conclusion
Though my wife told me, "You shouldn't be using errors to create functionality", it works great. There's a lot of things you can do with this, so use your imagination!
|
| |  | |  |
This document can be found in these Encyclopedia chapters: º Tricks of the Trade
|
 |
|
 |
This is what people have been saying about this article:
Create a new discussion |
 |
|
 |
View Article Statistics
Authors... Edit this article View Preview Version
|
|
| Printable Copy of Article |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
|
|