Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 6 votes, 4.50 average. Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 9th, 2006, 03:23 PM
Rust Indy Rust Indy is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 31 Rust Indy User rank is Private First Class (20 - 50 Reputation Level)Rust Indy User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 12 h 41 m 14 sec
Reputation Power: 3
How-To: URL Rewriting with ASP/IIS

Since I see the question around so often and have asked it myself for years, I thought I would post my solution to getting effective URL rewriting working in ASP.

For example, your current URLs look something like
Code:
http://www.mytestdomains.com/products.asp?id=342
, but you want them to look like
Code:
http://www.mytestdomains.com/products/342/
. For various reasons, the latter form is preferred by most people.

We'll assume for the time being that your existing site already works with the querystrings (although if you write a new web-app from scratch, you can ignore them completely).

First, create a new ASP file - we'll call ours "rewrite.asp". In that file, all it needs to do is grab the querystring that IIS will give it, strip our server name from it, and then store the remaining URL in an array. Once we have the array (or better, a Class with a property that reflects the array), we call
Code:
server.transfer
to transfer control to our existing files. This way, the browser (or robot, or whatever) will only see the URL they typed in, but the server will execute the page we want it to.

Example code for "rewrite.asp":
asp Code:
Original - asp Code
  1. Class URL
  2.  Private aURL ' the array that contains our URL elements
  3.  Private Sub Class_Initialize()
  4.   sTemp = Request.Querystring
  5.   ' the next line removes the HTTP status code that IIS sends, in the form "404;" or "403;" or whatever, depending on the captured error
  6.   sTemp = Right(sTemp, len(sTemp) - 4)
  7.   ' the next two lines remove both types of server names that IIS includes in the querystring
  8.   sTemp = replace(sTemp, "http://www.mytestdomains.com:80/", "")
  9.   sTemp = replace(sTemp, "http://www.mytestdomains.com/", "")
  10.   ' the next bit of code will force our array to have at least 1 element
  11.   If Right(sTemp, 1) <> "/" then
  12.    sTemp = sTemp & "/"
  13.   End If
  14.   aURL = split(sTemp, "/")
  15.  End Sub
  16.  Public Property Get url(index)
  17.   If index > uBound(aUrl) then
  18.    url = ""
  19.   Else
  20.    url = trim(aUrl(index))
  21.   End If
  22.  End Property
  23. End Class
  24.  
  25. Set MyUrl = New URL
  26.  
  27. Server.Transfer("/" & MyUrl.url(0) & ".asp?id=" & MyUrl.url(1))


Now, what this file does is receive a querystring from IIS that contains an error number (which we ignore here) and the URL that was asked for. We strip our server name from the URL and break the remainder into an array that we can reference from anywhere. This is a simple example, just taking the first element of the URL as the page name, and the second element as an ID of some sort. So we can request
Code:
http://www.mytestdomains.com/products/342
, and the server will show us
Code:
http://www.mytestdomains.com/products.asp?id=342
.

The final step requires that you create some custom errors in IIS.

Using the IIS manager (or your tool of choice), the following errors must be changed to have a message type of URL with "/rewrite.asp" as the URL.
  • 403;14 (Forbidden - Directory Listing Denied)
  • 404 (Not Found)
  • 405 (Method Not Allowed)
  • 500;15 (Internal Server Error - Direct requests for GLOBAL.ASA)
The 403, 404, and 500 errors should make sense - if someone requests a folder without a default document (that should be turned off, by the way, if you're rewriting URLs), requests a non-existent file or path, or tries to access the "global.asa" file, the "rewrite.asp" file should do it's magic. The 405 error is a little different - capturing that one and rewriting it lets POSTed form information be redirected as well.

I've implemented this solution (though slightly less simplified) a few times now with great success. I can't promise to be able to answer every single question (if anyone has any, that is), but I'll try.
Comments on this post
Shadow Wizard agrees: good stuff, thanks for sharing!
KiReSt agrees: Thanks, will probably be using this on my new site.

Last edited by Rust Indy : March 11th, 2006 at 08:18 PM. Reason: Title change

Reply With Quote
  #2  
Old March 9th, 2006, 06:22 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 46th Plane (27500 - 27999 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,737 Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 377762 Folding Title: Super Ultimate Folder - Level 1Folding Points: 377762 Folding Title: Super Ultimate Folder - Level 1Folding Points: 377762 Folding Title: Super Ultimate Folder - Level 1Folding Points: 377762 Folding Title: Super Ultimate Folder - Level 1Folding Points: 377762 Folding Title: Super Ultimate Folder - Level 1Folding Points: 377762 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 2 Weeks 5 h 41 m 46 sec
Reputation Power: 1914
welcome to aspfree and thanks for sharing, your post has been moved
to the Code Bank.

Reply With Quote
  #3  
Old March 11th, 2006, 08:18 PM
Rust Indy Rust Indy is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 31 Rust Indy User rank is Private First Class (20 - 50 Reputation Level)Rust Indy User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 12 h 41 m 14 sec
Reputation Power: 3
Thanks!

It should also be noted that since ASP 3 (IIS5 and up) supports regular expressions, this class could be rewritten to handle MOD_REWRITE-like rules. Someone with more motivation (and understanding of regexps) will have to do it though.

Reply With Quote
  #4  
Old May 3rd, 2006, 02:40 AM
Blackwater Blackwater is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 Blackwater User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 53 sec
Reputation Power: 0
Hello,
I really like your code and it looks like it will help me achieve exactly what I'm trying to do.

Unfortunately, it breaks right at the end.

Quote:
Originally Posted by Rust Indy
Server.Transfer("/" & MyUrl.url(0) & ".asp?id=" & MyUrl.url(1))


Server.Transfer cannot handle querystrings ("?")!

How did you get it to work?

Many thanks,
Blackwater

Reply With Quote
  #5  
Old May 3rd, 2006, 09:00 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 121
That's a good question.

Personally, I do not like this implementation because basically you are redirecting all of your site traffic through your error pages.
They are there for a reason. It also messes up your logs.

Reply With Quote
  #6  
Old May 3rd, 2006, 09:29 AM
Blackwater Blackwater is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 Blackwater User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 53 sec
Reputation Power: 0
Quote:
Originally Posted by degsy
That's a good question.

Personally, I do not like this implementation because basically you are redirecting all of your site traffic through your error pages.
They are there for a reason. It also messes up your logs.


Thanks for the reply.

I managed to get around it by putting the querystrings in session variables - not the ideal solution, but it's working.

A agree about it messing up your logs.. what other options are there?

Cheers,
Blackwater

Reply With Quote
  #7  
Old May 8th, 2006, 06:31 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 121
There are utilities available that you can install that will act like Apache Mod_Rewrite.

http://www.google.co.uk/search?hl=en&q=IIS+url+rewrite&meta=

Reply With Quote
  #8  
Old June 9th, 2006, 02:59 AM
panblik panblik is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Posts: 1 panblik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 57 sec
Reputation Power: 0
confused!!!!!!!

I have gone through your code. But I have some problem regarding URL rewriting in ASP. I have created a file “rewrite.asp” and place your code there. Now the problem is that I cannot able to place this code into its exact place in my application to get desired output. Please help me how and where to implement this code in my application soon. One more things is that I have no “global.asa” .


looking forward to your reply soon

Reply With Quote
  #9  
Old June 13th, 2006, 05:07 AM
D.O.M.I.N.A.T.O.R's Avatar
D.O.M.I.N.A.T.O.R D.O.M.I.N.A.T.O.R is offline
Kingpin contributor
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Nov 2005
Location: P.E, RSA
Posts: 1,051 D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level)D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level)D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level)D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level)D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level)D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level)D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level)D.O.M.I.N.A.T.O.R User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 2 h 12 m 16 sec
Reputation Power: 201
Very usefull Rust Indy.

Consider looking at how easy this is to achieve with URL Mapping in ASP.NET 2.0

ASP.NET 2.0: URL Mapping with RegEx Support

Jo.
__________________
Fitness & Diet resources Career Descriptions Boat Cruises
All code that is posted by me has not been tested, and it should only be interpreted as a guideline to a solution. There is no guarantee that any of my code samples will work as provided, and should be customized to suite the required need.

Last edited by D.O.M.I.N.A.T.O.R : June 13th, 2006 at 05:10 AM.

Reply With Quote
  #10  
Old August 22nd, 2006, 05:19 AM
albertindian albertindian is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 165 albertindian User rank is Corporal (100 - 500 Reputation Level)albertindian User rank is Corporal (100 - 500 Reputation Level)albertindian User rank is Corporal (100 - 500 Reputation Level)albertindian User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 19 h 5 m 40 sec
Reputation Power: 4
Send a message via Yahoo to albertindian
You can even use isapirewrite component which is free (lite version). Currently my site http://www.bepenfriends.com is using that for rewriting the URL. THis won't fill the log file with error codes and it is comeratively fast. easy to configure. single line of regEx will change everything.

http://www.helicontech.com/ is the website

/products/(\d+) /product.asp\?ID=$1 [I,L] will do the same with the same directory format which you specified in the article.

But the only problem is this will not work if you have a shared host because in a shared hosting you are not having root access to install the control.

Albert
http://www.bepenfriends.com

Reply With Quote
  #11  
Old August 28th, 2006, 09:10 PM
baseballdude_'s Avatar
baseballdude_ baseballdude_ is offline
Expert Learner
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2005
Location: Wisconsin
Posts: 1,891 baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)  Folding Points: 22104 Folding Title: Starter FolderFolding Points: 22104 Folding Title: Starter Folder
Time spent in forums: 1 Week 5 Days 13 h 45 m 4 sec
Reputation Power: 70
Send a message via AIM to baseballdude_ Send a message via MSN to baseballdude_ Send a message via Yahoo to baseballdude_ Send a message via Google Talk to baseballdude_
I have used ISAPI Rewrite as well. The benefit out of the 404 rewrite that I have noticed is that the rewrites can be dynamic...you can use loops and such in the 404.asp file. It's pretty neat.

Reply With Quote
  #12  
Old August 28th, 2006, 09:24 PM
baseballdude_'s Avatar
baseballdude_ baseballdude_ is offline
Expert Learner
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2005
Location: Wisconsin
Posts: 1,891 baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)