Tuesday, 5 February 2013

ASP: URL Rewrite and IIS7

   


Following a kind suggestion by a follower, today we are going to see how to use IIS URL Rewrite 2.0 for IIS 7.

First of all, what's URL Rewrite?
"IIS URL Rewrite 2.0 enables Web administrators to create powerful rules to implement URLs that are easier for users to remember and easier for search engines to find. By using rule templates, rewrite maps, .NET providers, and other functionality integrated into IIS Manager, Web administrators can easily set up rules to define URL rewriting behavior based on HTTP headers, HTTP response or request headers, IIS server variables, and even complex programmatic rules. In addition, Web administrators can perform redirects, send custom responses, or stop HTTP requests based on the logic expressed in the rewrite rules."

Rest assured, we can use it for a classic ASP site as well.
What we need to do is:
1) install URL Rewrite after downloading it from the Ms IIS site;
2) enable ASP.NET role service on IIS 7;
3) write a rule



Nothing very complicated. What we will focus on in the following post is how to write appropriate rules in order to change URL.


The use of URL Rewrite is quite simple. We need to open IIS Manager, select the Default Web Site and in the Feature View click on URL Rewrite. At that point, on the right panel we will be able to click on Add Rules and select Blank rule... and here starts the magic.

The next panel has all we need to use to set up a new rule.
First of all give a name to your new rule. After that we fill in the Match URL section. That is where we set what to look for or the URL we want to change.

Remember when we talked about Regular Expressions? No? Well go and see what we discussed on this post. We need to refresh those ideas because we need to use Regular Expressions to fill in the Match URL section.
For "Requested URL" select "Matches the pattern" and for "Using" select "Regular Expression". In the pattern filed insert the regular expression used to search for a candidate URL. This is where we create the rule used to match an URL to be changed to something else.
Some part of our patter are between parentheses which creates capture groups referenced in the "Action" section.
Now, select "Ignore Case" and go to the Action section.
Here is where we decide what the rule should do when a match is found. Because we want to rewrite the URL, we select "Rewrite" in the first list box. In the "Action properties" we need to write the way we want the URL to appear. Select "Append query string" and then apply.

As an example, we can suppose that our URL is like:
http://localhost/article/342/some-article-title
and we want it to appear as:
http://localhost/article.aspx?id=342&title=some-article-title
How?
It all depends on our RegEx. In order to match the URL we can use something like:
^article/([0-9]+)/([_0-9a-z-]+)
as explained on the IIS Ms site.
In the "Action properties" we can use the following expression:
article.aspx?id={R:1}&title={R:2}
Forget about the .aspx extension. The important thing here is the use of the capture groups (R1 and R2) which are defined in the match pattern (first RegEx above - see the parentheses?).

As you can see it is not very complicated. Or, just to be clear enough, it all depends on RegEx and we all know they are not really easy to understand.
However, there are many interesting tools on the web that can help us to validate RegEx like RegExr. All we really need to do is understand how Regular Expressions work.

Last but not least, I have to say I've never used URL Rewrite, but in order to help our kind reader who ask information about it, I asked some friends who have used and who are still using URL Rewrite: they told me it's perfectly working and, provided you know RegEx, it is quite easy to set up.

Enjoy and let me know what you think!

1 comment:

  1. At work we're starting a project with URL writing in II6. It's proving to be quite difficult as there's many different parameters in the url to consider.

    ReplyDelete

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.