Sitecore URL Rewrite Backreferences

Image

Last night, my colleague John Kurtis inquired about the Sitecore URL Rewrite module and asked:

Hey – I’m trying to redirect

Downloads/autoupdate/* to the same path on another domain. 

Eg

Downloads/autoupdate/some folder/foo.xml would go to http://www.domain.com/downloads/autoupdate/some folder/foo.xml

How can I do that with the module?

There are 2 kinds of backreferences, each one with its own token format:

  • Rule – {R:n} – pattern matches in the rule
  • Condition – {C:n} – pattern matches in the condition

First, you need to understand regex capture groups.  A regex capture group is signified by a parenthesis in the regex.  If you head over to www.regexr.com and enter /foo(.*)bar/g as the expression, you can see what I’m talking about:

pic1

Here are the matches explained.

When it encounters “foo@demo.net     bar”, the capture group is “@demo.net    “:

pic2

When it encounters “foo.html?q=bar”, the capture group is “.html?q“:

pic3

In the URL Rewrite module, the {R:0} would signify use the rule pattern match’s first capture group.  Remember, this is the information age and things are of a zero-based index.

Using the URL Rewrite module, I can create the following rule to redirect everything to a different domain that is under a specific subpath:

In the rule, specify the pattern as downloads\/autoupdate\/foo(*).  This means to match on url’s where the path begins with downloads/autoupdate/foo and ends with anything.

pic4

In the redirect, specify the url to be http://mynewdomain/mynewpath/downloads/autoupdate/foo{R:0}.  This means to redirect to the new url, but replace the backreference token with the matched capture group from the rule.

pic5

Using my simlulation tool, you can see it work as follows:

pic6

Remember, my module was based on IIS Url Rewrite, so if you have any other questions you can use the IIS Url Rewrite documentation.  Here is the specific link to IIS URL Rewrite back reference configuration.  Alternatively, you can contact me directly.