Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34180

IIS UrlRewrite with optional parameters

I want to rewrite a url like:

/categories/optional-sub-cat/product

or

/categories/optional-sub-cat/optional-2nd-sub-cat/product

How should I write the regular expression to include optional parameters?

  <rule name="Products" enabled="true" stopProcessing="true">
          <match url="/(.+)/optional-sub-cat/optional-2nd-sub-cat/product" />
          <action type="Rewrite" url="products.aspx?cat={R:1}&amp;sub=..." />
        </rule> 

Upvotes: 1

Views: 1767

Answers (1)

user92454
user92454

Reputation: 1211

I haven't tested this, but something like this should work ([^/] basically says match everything other than / so that the first .+ doesn't eat up all matches and produce it in R1):

 <rule name="Products" enabled="true" stopProcessing="true">
      <match url="/([^/]+)/([^/]*/)?([^/]*/)?product" />
      <action type="Rewrite" url="products.aspx?cat={R:1}&amp;sub1={R:2}&amp;sub2={R:2}" />
    </rule> 

Upvotes: 2

Related Questions