Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite {R:N} clarification

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

like image 803
Nil Pun Avatar asked Jun 08 '13 11:06

Nil Pun


People also ask

What is r1 URL Rewrite?

If we apply for example the URL http://website.com/github to this rule, {R:1} will contain github . It means that combined with the action, a user reaching http://website.com/github will ultimately be redirected to https://github.com.

How does IIS URL Rewrite work?

The URL rewriting module runs early in the request-processing pipeline, modifying the requested URL before the Web server decides which handler to use to process the request. The handler, which is chosen based on the rewritten URL, processes the request and generates a response that is sent back to the Web browser.

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.


1 Answers

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

like image 172
cheesemacfly Avatar answered Sep 22 '22 12:09

cheesemacfly