aw1975
aw1975

Reputation: 1708

IIS URL rewrite rule using conditions

I have the following URL rewrite rule configured in my web.config file:

<rule name="Test Rule" stopProcessing="true">
  <match url="^$" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="^(item=1|all|none|first).*$" />
  </conditions>
  <action type="Rewrite" url="/newsite/test.asp?{C:0}" />
</rule>     

The following source url matches as expected.

http://domainname?item=1&param1=x%param2=y

However the rewritten url includes param1 and param2 in its query string. Shouldn't {C:0} only translate to what is matched in the regex group i.e. (item=1|all|none|first) and not anything that comes after it?

Upvotes: 3

Views: 5896

Answers (1)

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47159

The reason why the rewrite captures everything after the ( ) is because {C:0} grabs the entire match, and not just the group. Since you only want to capture what is within the ( ) (which is capture group 1) you would use:

{C:1}

Another example might be:

^(item=1|all|none|first)/(abc).*$

So to capture only abc for example (capture group 2):

{C:2}

Upvotes: 2

Related Questions