| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
#1
|
|||||
|
|||||
|
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 Code:
http://www.mytestdomains.com/products/342/ 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 Example code for "rewrite.asp": asp Code:
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 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.
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. Last edited by Rust Indy : March 11th, 2006 at 08:18 PM. Reason: Title change |
|
#2
|
||||
|
||||
|
welcome to aspfree and thanks for sharing, your post has been moved
to the Code Bank. ![]() |
|
#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. |
|
#4
|
|||
|
|||
|
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:
Server.Transfer cannot handle querystrings ("?")! How did you get it to work? Many thanks, Blackwater |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
Quote:
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 |
|
#7
|
|||
|
|||
|
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= |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
||||
|
||||
|
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. |
|
#10
|
|||
|
|||
|
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 |
|
#11
|
||||
|
||||
|
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.
__________________
www.xoise.com - www.ourfreegames.com - www.g1games.com - www.randomtools.net - www.xenocide-rpg.com |
|
#12
|
||||
|