【问题标题】:IIS Rewrite rule based on length param基于长度参数的 IIS 重写规则
【发布时间】:2016-02-12 13:57:42
【问题描述】:

我有以下重写规则

<rule name="Product short redirect" stopProcessing="true">
      <match url="product/([A-Za-z0-9]+)/$" ignoreCase="true" />
      <action type="Redirect" redirectType="Permanent"  url="product-redirect/?code={R:1}" />
    </rule>

但我只希望它匹配长度超过 3 个字符的产品代码

<rule name="Product short redirect" stopProcessing="true">
      <match url="product/([A-Za-z0-9].{4}+)/$" ignoreCase="true" />
      <action type="Redirect" redirectType="Permanent"  url="product-redirect/?code={R:1}" />
    </rule>

但这仅返回部分匹配,并且三个字符代码仍然匹配??

部分网址示例如下:

product/u22tfp1/

product/xxx/

【问题讨论】:

    标签: url iis url-rewriting rewrite


    【解决方案1】:

    如果你只想匹配4个字符或更多的产品,你需要在正则表达式上指定长度:

    E.G:想要匹配 (products/1234/ OR products/12345/)

    <match url="product\/([A-Za-z0-9]{4,100}+)\/$" />
    

    我在这个例子中使用了 4 到 100 之间的匹配(你也可以明确避免匹配 3 个字符,但在我看来它看起来更丑)

    注意: 前面的正则表达式product/([A-Za-z0-9].{4}+)/$ 的问题是点字符. 匹配所有内容,基本上你说的是匹配:

     "product/" then 
     "a single character/digit [a-Z0-9]" then 
     "anything with a length of 4"(repeat this last statement "+" ) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-21
      • 2021-05-10
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      相关资源
      最近更新 更多