IIS基于长度参数重写规则 [英] IIS Rewrite rule based on length param
问题描述
我有以下重写规则
<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个字符的产品代码
However i want it only to match product codes that are longer than 3 characters
<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>
但这只返回部分匹配,而且三个字符代码仍然匹配?
But this only returns a partial match and also the three characters codes still match ??
部分网址示例如下:
product/u22tfp1/
和
product/xxx/
推荐答案
如果您只想匹配那些产品是4个字符或更多,你需要在正则表达式上指定长度:
If you want to only match products that are 4 characters or more, you need to specify the length on the regular expression:
EG:想要匹配(产品/ 1234 /
或产品/ 12345 /
)
E.G: Want a match of (products/1234/
OR products/12345/
)
<match url="product\/([A-Za-z0-9]{4,100}+)\/$" />
我在这个例子中使用了4到100之间的匹配(你也可以明确地避免与3匹配)字符,但在我看来看起来更丑陋)
I use a match between 4 and 100 on this example (you can also explicitly avoid a match with 3 characters, but it looks uglier on my opinion)
注意:
前一个正则表达式的问题 product /([A- Za-z0-9]。{4} +)/ $
是匹配所有内容的点字符。
,基本上你说匹配:
NOTE:
The problem with the previous regular expression product/([A-Za-z0-9].{4}+)/$
is the dot character .
that matches everything, basically you are saying match:
"product/" then
"a single character/digit [a-Z0-9]" then
"anything with a length of 4"(repeat this last statement "+" )
这篇关于IIS基于长度参数重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!