IIS重定向规则用于不确定的子目录

huangapple go评论51阅读模式
英文:

IIS Redirect rule for uncertain sub-directory

问题

我需要编写一条规则来实现以下目标:

<rule name="Redirect Category No Parent" stopProcessing="true">
   <match url="^content/category/([a-zA-Z\d-_\s]+)(/[a-zA-Z\d-_\s]+)*" />
   <action type="Rewrite" url="page?isCategory=true&amp;categories={R:1}&amp;other={R:2}&amp;all={R:0}" appendQueryString="true" />
</rule>

但它没有给我整个路径,只是给了我这个:

isCategory=true&amp;categories=xxx&amp;other=/zzz&amp;all=content/category/xxx/yyy/zzz

对于此输入:

https://localhost:44354/content/category/xxx/yyy/zzz

有人知道为什么"other"是"/zzz",我期望它是"/xxx/yyy/zzz"。

英文:

I need to write a rule to achieve these:

&lt;rule name=&quot;Redirect Category No Parent&quot; stopProcessing=&quot;true&quot;&gt;
   &lt;match url=&quot;^content/category/([a-zA-Z\d-_\s]+)(/[a-zA-Z\d-_\s]+)*&quot; /&gt;
   &lt;action type=&quot;Rewrite&quot; url=&quot;page?isCategory=true&amp;amp;categories={R:1}&amp;amp;other={R:2}&amp;amp;all={R:0}&quot; appendQueryString=&quot;true&quot; /&gt;
&lt;/rule&gt;

but it doesn't give me all the path it just gives me this
**

> isCategory=true&categories=xxx&other=/zzz&all=content/category/xxx/yyy/zzz

**
for this input

> https://localhost:44354/content/category/xxx/yyy/zzz

anybody knows why other is /zzz I expect it to be /xxx/yyy/zzz

答案1

得分: 1

你规则中的问题在于<match>元素内部的正则表达式。在示例URL中有多个片段(xxx、yyy、zzz),如果你想捕获多个片段,可以将正则表达式模式修改如下:

&lt;match url=&quot;^content/category(/([a-zA-Z\d-_\s]+)((/[a-zA-Z\d-_\s]+)*))&quot; /&gt;

对于这个输入:

&gt; https://localhost:44354/content/category/xxx/yyy/zzz

你将会得到以下捕获组:

{R:0}   content/category/xxx/yyy/zzz
{R:1}   /xxx/yyy/zzz
{R:2}   xxx
{R:3}   /yyy/zzz
{R:4}   /zzz

如果你想要获得 categories=xxxother=/xxx/yyy/zzz,你可以像这样修改重写URL:

&lt;action type=&quot;Rewrite&quot; url=&quot;page?isCategory=true&amp;amp;categories={R:2}&amp;amp;other={R:1}&amp;amp;all={R:0}&quot; appendQueryString=&quot;true&quot; /&gt;

这将给你所有你想要的路径:

&gt; isCategory=true&amp;categories=xxx&amp;other=/xxx/yyy/zzz&amp;all=content/category/xxx/yyy/zzz
英文:

The problem in your rule is the regular expression inside the <match> element. There are multiple segments (xxx, yyy, zzz) in the example URL, if you want to capture multiple segments, you can modify the regex pattern as follows:

&lt;match url=&quot;^content/category(/([a-zA-Z\d-_\s]+)((/[a-zA-Z\d-_\s]+)*))&quot; /&gt;

For this input:

> https://localhost:44354/content/category/xxx/yyy/zzz

You will get the following capture groups:

{R:0}   content/category/xxx/yyy/zzz
{R:1}   /xxx/yyy/zzz
{R:2}   xxx
{R:3}   /yyy/zzz
{R:4}   /zzz

If you want to get categories=xxx and other=/xxx/yyy/zzz
you can modify the rewrite URL like this:

&lt;action type=&quot;Rewrite&quot; url=&quot;page?isCategory=true&amp;amp;categories={R:2}&amp;amp;other={R:1}&amp;amp;all={R:0}&quot; appendQueryString=&quot;true&quot; /&gt;

It will give you all the path you want:

> isCategory=true&categories=xxx&other=/xxx/yyy/zzz&all=content/category/xxx/yyy/zzz

huangapple
  • 本文由 发表于 2023年6月15日 21:58:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定