O'Reilly logo

Professional Microsoft IIS 8 by Benjamin Perkins, Dennis Glendenning, Scott Forsyth, Jeff Cochran, Kenneth Schaefer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Back-References

URL Rewrite rules often make use of back-references. These are references to information obtained from earlier in the rule. If you're jumping right to this section in the chapter, you might want to review the previous sections, which discuss the regex syntax for back-references.

Rule Back-References versus Condition Back-References

URL Rewrite has two types of back-references: rule back-references and condition back-references. A rule back-reference is taken from the Match URL pattern part of the rule, whereas a condition back-reference is taken from the conditions. A rule back-reference is identified by {R:N}, where N is from 0 to 9, and a condition back-reference is identified in a similar way by {C:N}.

To use back-references, you must first capture the data, and then you can use the data. Before explaining further, let's look at an example.

<rule name="Subdomain redirect" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="ˆwww\.localtest\.me$" negate="true" />
        <add input="{HTTP_HOST}" pattern="ˆ([ˆ\.]+)\.localtest\.me$" />
    </conditions>
    <action type="Redirect" url="http://localtest.me/{C:1}/{R:0}" />
</rule>

This rule watches for a URL like sub.localtest.me/article.aspx and redirects to localtest.me/sub/article.aspx. This is a good example of back-references because it makes use of both types.

The rule back-reference is identified by {R:0}. In the preceding example, the <match url=".*” /> is the rule, and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required