URL Rewrite for IIS 7.0 / Regular Expressions and Wildcards

Last week we shipped the first preview of URL Rewrite Module for IIS 7.0. If you are not familiar with URL Rewriting engines, please check out the walkthroughs:

 https://learn.iis.net/page.aspx/460/using-url-rewrite-module/.

If you are you familiar with mod_rewrite, I’m pleased to let you know that we have a mod_rewrite rule importer feature to help you to leverage your rule-crafting knowledge:

 https://learn.iis.net/page.aspx/470/importing-apache-modrewrite-rules/

URL Rewrite Module ships two matching engines, one for Regular Expressions (Perl 5 compatible/ECMAScript) and a simple one for wildcard matching. You can choose the engine you feel more comfortable with, I just want to give you some facts (some of them are silly but they are just a reminder):

1. Regex matching is more powerful, but expensive in terms of execution time.

2. Wildcard matching is cheaper than Regex and easy to understand.

3. Both engines are case-insensitive by default.

4. Case-sensitive matching has much better performance than case-insensitive.

5. Wildcard matching assumes “exact match”. For an expression like “blog”, it would match “blog” and only “blog”.

6. Regex matching assumes “partial match”. In this case, “blog” will match “blogger”, “demo/blog”. If you want an exact match you need to change the match expression to “^blog$”.

7. In Regex, “.” is reserved, it means “any character”, so “default.aspx$” will match “defaultXaspx” too. Escape it to “default \ .aspx$’. Wildcards doesn’t have this behavior, the only reserved character is ‘*’.

8. To create a capture in Regex, use parenthesis.

9. Each ‘*’ in a wildcard expression creates a capture. For example, an expression like “*.*” would match “logo.jpg” and it will create 2 captures, “logo” and “jpg” (without the dot).

10. You can reference a capture by index. The index is 1-based, so in the previous example 1 means “logo” and 2 means “jpg”.

11. The index 0 means “the entire input”. In the previous example 0 means “logo.jpg”.

12. The syntax for back-reference a capture is {type:index} where type can be R for “Rule Back References” and “ C for “Condition Back References”. The index must be and integer equals or greater than zero.

Please give all the feedback you want, this is a technical preview so your feedback can definitely make a difference for the next release.

Install it from:

Microsoft URL Rewrite Module for IIS 7.0 CTP1 (x86)

Microsoft URL Rewrite Module for IIS 7.0 CTP1 (x64)

URL Rewrite Forum:

https://forums.iis.net/1152.aspx

Comments

  • Anonymous
    December 14, 2011
    can you  please,  explain with example {C:0} {C:1} {R:0} {R:1} ??

  • Anonymous
    July 25, 2013
    I have requested url and If i add slash then it change the requested url ,what I need to change..

  • Anonymous
    August 13, 2014
    {C:N} = Condition Back Reference {R:N} = Rewrite Back Reference Condition: {QUERY_STRING} Matches the Pattern Pattern: (.*) Ignore Case: [Checked] {C:1} will equal your querystring, now subsequent matches and back references depend on if you have "track capture groups across conditions" checked.. if not index 1 will be whatever the last matched condition is, if it is checked each back reference can be used by index of occurrence. Very similar for your rewrite pattern matching..except it should always be "tracking the capture group"