简体   繁体   English

带有可选参数的IIS UrlRewrite

[英]IIS UrlRewrite with optional parameters

I want to rewrite a url like: 我想像这样重写一个URL:

/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> 

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): 我还没有测试过,但是类似这样的东西应该可以工作([^ /]基本上说匹配/以外的所有东西,以便第一个。+不会吃光所有匹配并在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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM