Tip #31: Did you know... How to have user friendly URL using IIS 7.0 URL Rewrite module?
It's always good to have friendly URL so that people visiting your site find it easier to remember. Asking your user to remember http://www.contoso.com/article.aspx?id=342&title=URL-Rewrite-Walkthrough for URL Rewrite walkthrough is not the nicest thing to do. Won't it be cool if user can be provided with URL like http://www.contoso.com/article/342/URL-Rewrite-Walkthrough. This indeed resonates better and is much easier to remember.
With URL Rewrite module in IIS 7.0 you can easily build this logic without having to modify your code using Rewrite Maps. I will tell you how to do this from IIS Manager. Open IIS Manager and select "Default Web Site". In the feature view click "URL Rewrite and click "Ass Rules..." in the "Actions" pane. Select "Blank Rule" to start with. This will open up the "Edit Rule" property page. In order to define actual rewrite rule following has to be done:
In order to make the above example work the "Edit Rule" property page should look like:
The name of the rule should be unique for the rule. The pattern string "^article/([0-9]+)/([_0-9a-z-]+)" is a regular expression and will match any URL string that satisfies the below criteria:
Since we created a rule which is supposed to rewrite URL, the rule type is "Rewrite". The Rewrite URL string "article.aspx?id={R:1}&title={R:2}" specifies new value to which input URL should be rewritten. The query parameters we used {R:1} and {R:2} are back reference to get groups as defined by rule pattern. You can learn more about back reference here. To learn more about this see this walkthrough.
Don Raman
SDET, IIS Team