99

I've not been able to understand the purpose of {R:N}. Could anyone please clarify when to use
{R:0} vs. {R:1}

usage example:

<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />

I've seen ScottGu using {R:1}

http://weblogs.asp.net/scottgu/archive/2010/04/20/tip-trick-fix-common-seo-problems-using-the-url-rewrite-extension.aspx

Whereas, below has {R:0}

http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

Had a look at the IIS link below but could not quite digest the definition below:

Back-references to condition patterns are identified by {C:N} where N is from 0 to 9; back-references to rule pattern are identified by {R:N} where N is from 0 to 9. Note that for both types of back-references, {R:0} and {C:0}, will contain the matched string

Andrew
  • 18,680
  • 13
  • 103
  • 118
Nil Pun
  • 17,035
  • 39
  • 172
  • 294

1 Answers1

118

As per the documentation:

When an ECMAScript pattern syntax is used, a back-reference can be created by putting parenthesis around the part of the pattern that must capture the back-reference.

So taking the example that follows in the documentation:

^(www\.)(.*)$

And using the input string www.foo.com in the conditions, you will have:

{C:0} - www.foo.com
{C:1} - www.
{C:2} - foo.com

To make it simple:

  • {R:x} is used as back reference from the rule pattern (<match url="...">).
  • {C:x} is used as back reference from the condition pattern (<conditions><add input="{HTTP_HOST}" pattern="..."></conditions>)
  • The 0 reference contains the whole input string
  • The 1 reference will contain the first part of the string matching the pattern in the first parenthesis (), the 2 reference the second one, etc...up to the reference number 9

Note:

When "Wildcard" pattern syntax is used, the back-references are always created when an asterisk symbol (*) is used in the pattern. No back-references are created when "?" is used in the pattern.

http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Using_back-references_in_rewrite_rules

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • 12
    In case it helps someone, this also is a related and a very helpful link: https://nicolas.guelpa.me/blog/2015/02/21/rewrite-redirect-iis.html – niki b Jun 27 '18 at 20:53
  • 2
    @niki b: your blog did help me a lot because of what you say explicitly "_Important: The rule is only applied to the path; don’t let the name url fool you. (for example, in http://example.com/test, the scheme and domain name are ignored for the “url” matching)_", and "_Always remember when you debug a redirect (specifically a 301) that browsers tend to cache them and that it can lead to frustration when you change the rule but nothing happens…_". Please correct you example with the domain name using the {R:X} because it is confusing, we don't have access to it with the Rules. – Daniel Lobo Oct 03 '19 at 21:54
  • @DanielLobo I am glad the blog helped you. I did not write it though so if you want, you can contact the blogger directly. But it's good you pointed it out here for people to be aware of that. – niki b Oct 07 '19 at 15:43