Reputation: 34180
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}&sub=..." />
</rule>
Upvotes: 1
Views: 1767
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}&sub1={R:2}&sub2={R:2}" />
</rule>
Upvotes: 2