« Previous Next »

Thread: Pulling Backreferences from Multiple Conditions

Last post 10-13-2008 9:47 PM by speednet. 2 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (3 items)

Sort Posts:

  • 10-13-2008, 4:56 PM

    • speednet
    • Not Ranked
    • Joined on 06-23-2005, 11:35 PM
    • Posts 7

    Pulling Backreferences from Multiple Conditions

    I am running into a problem when trying to construct a rewritten URL that contains backreferences to more than one condition.

    I am trying to match more than one QueryString variable, and the complicating factor with QueryStrings is that you never know the order the variables will be in.  So I am using one condition per QueryString variable.

    For example:

    <conditions>
        <add input="{QUERY_STRING}" pattern="(?:^|&amp;)var1=([1-9]\d+)(?:&amp;|$)" />
        <add input="{QUERY_STRING}" pattern="(?:^|&amp;)var2=([1-9]\d+)(?:&amp;|$)" />
    </conditions>

    My assumption was that the first condition would make {C:0} and {C:1} available, and the second condition would make {C:2} and {C:3} available.

    Thus, my rewrite rule looks like this:

    <action type="Redirect" url="newpage.aspx/{C:1}/{C:3}" appendQueryString="false" redirectType="Permanent" />

    However, the URL Rewriter tool gives me the error message, "Invalid condition back reference. The index 3 is invalid."

    So how am I supposed to access the backreferences in all the conditions that come after the first one?

    Thanks very much for your help!

  • 10-13-2008, 5:42 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 660

    Re: Pulling Backreferences from Multiple Conditions

    Within a rule action, you can use the back-references to the the last matched condition of that rule. Also within a condition input string, you can use the back-references to the previously matched condition.

    With that in mind, a possible way to do what you are trying to do is to use the back-reference from previous condition in the next condition's input:

    <conditions>
        <add input="{QUERY_STRING}" pattern="(?:^|&amp;)var1=([1-9]\d+)(?:&amp;|$)" />
        <add input="%{C:1}%_{QUERY_STRING}" pattern="%(.+)%_.*var2=([1-9]\d+)(?:&|$)" />
    </conditions>

    Then the action of the rule will look like this:

    <action type="Redirect" url="newpage.aspx/{C:1}/{C:2}" appendQueryString="false" redirectType="Permanent" />

    http://ruslany.net
  • 10-13-2008, 9:47 PM In reply to

    • speednet
    • Not Ranked
    • Joined on 06-23-2005, 11:35 PM
    • Posts 7

    Re: Pulling Backreferences from Multiple Conditions

    GREAT reply, thank you very much ruslany!  That is incredibly helpful, and not something I was able to locate on any of the support pages.

Page 1 of 1 (3 items)
Microsoft Communities