英文:
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&categories={R:1}&other={R:2}&all={R:0}" appendQueryString="true" />
</rule>
但它没有给我整个路径,只是给了我这个:
isCategory=true&categories=xxx&other=/zzz&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:
<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>
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),如果你想捕获多个片段,可以将正则表达式模式修改如下:
<match url="^content/category(/([a-zA-Z\d-_\s]+)((/[a-zA-Z\d-_\s]+)*))" />
对于这个输入:
> 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=xxx
和 other=/xxx/yyy/zzz
,你可以像这样修改重写URL:
<action type="Rewrite" url="page?isCategory=true&amp;categories={R:2}&amp;other={R:1}&amp;all={R:0}" appendQueryString="true" />
这将给你所有你想要的路径:
> isCategory=true&categories=xxx&other=/xxx/yyy/zzz&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:
<match url="^content/category(/([a-zA-Z\d-_\s]+)((/[a-zA-Z\d-_\s]+)*))" />
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:
<action type="Rewrite" url="page?isCategory=true&amp;categories={R:2}&amp;other={R:1}&amp;all={R:0}" appendQueryString="true" />
It will give you all the path you want:
> isCategory=true&categories=xxx&other=/xxx/yyy/zzz&all=content/category/xxx/yyy/zzz
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论