在IIS7中重写映射 - 如何使匹配可选地包含尾部斜杠? [英] Rewrite maps in IIS7 — how to make the match optionally include a trailing slash?
问题描述
我已经阅读了几个 IIS重写地图条件
等组合的前30个Google点击次数,但我找不到任何体面的文档,无论是在microsoft.com网站还是其他地方。
I have read the top 30 Google hits for several combinations of IIS rewrite map condition
and so on, but I can't find any decent documentation, either on a microsoft.com site or elsewhere.
我在IIS7中有一堆重写地图,我想处理它们,无论它们是否存在之后是斜线。所以 www.foo.com/bar
和 www.foo.com/bar /
都应符合规则。
I have a bunch of rewrite maps in IIS7 that I would like to process irrespective of whether or not they are followed by a trailing slash. So www.foo.com/bar
and www.foo.com/bar/
should both match the rule.
<rewrite>
<rewriteMaps>
<rewriteMap name="ShortURLs">
<add key="/terms" value="/en-us/terms-and-conditions/"/>
<add key="/privacy" value="/en-us/privacy-and-cookies/"/>
<add key="/buy" value="/en-us/where-to-buy/"/>
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Short URL redirects">
<match url="^/?(.+)/?$" />
<conditions>
<add input="{ShortURLs:{REQUEST_URI}}" pattern="(.+)"/>
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true"/>
</rule>
</rules>
</rewrite>
现在这个效果非常好,除了我能找到的唯一方法 / terms /
匹配重写映射中的第一个键是复制映射,使其显示为:
Now this works perfectly well, except that the only way I can find to make /terms/
match the first key in the rewrite map is to duplicate the map, so that it reads:
<rewriteMap name="ShortURLs">
<add key="/terms" value="/en-us/terms-and-conditions/"/>
<add key="/privacy" value="/en-us/privacy-and-cookies/"/>
<add key="/buy" value="/en-us/where-to-buy/"/>
<add key="/terms/" value="/en-us/terms-and-conditions/"/>
<add key="/privacy/" value="/en-us/privacy-and-cookies/"/>
<add key="/buy/" value="/en-us/where-to-buy/"/>
</rewriteMap>
考虑到我首先使用正则表达式来匹配它们,这看起来非常荒谬。将 /?
添加到条件输入或条件模式似乎不起作用。
This seems ridiculously inelegant, given that I'm using regular expressions to match them in the first place. Adding /?
to the condition input or the condition pattern doesn't seem to work.
我见过< a href =https://stackoverflow.com/a/13630398/205245>答案 IIS7重写地图正则表达式?提到正则表达式不能使用(引自在URL重写模块中使用重写映射)但是,正如我在那里评论的那样,这似乎与在该文本之前给出的具体示例有关,而不是批发这可以永远不会工作。
I have seen the answer to IIS7 Rewrite Map Regex? that mentions regular expressions cannot be used (quoting from Using Rewrite Maps in URL Rewrite Module) but, as I have commented there, this seems to relate to the specific examples being given before that text, rather than a wholesale "this can never work".
我缺少什么?必须有一些方法来做到这一点;我错过了一些明显的东西吗?
What am I missing? There must be some means of doing this; am I missing something obvious?
推荐答案
这应该这样做:
<rewrite>
<rewriteMaps>
<rewriteMap name="ShortURLs">
<add key="terms" value="/en-us/terms-and-conditions/"/>
<add key="privacy" value="/en-us/privacy-and-cookies/"/>
<add key="buy" value="/en-us/where-to-buy/"/>
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Short URL redirects">
<match url="^(.+?)/?$" />
<conditions>
<add input="{ShortURLs:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true"/>
</rule>
</rules>
</rewrite>
你非常接近;我只需做三个小改动:
You were quite close; I only needed to make three small changes:
- 删除重写地图中键中的前导斜杠
- 在规则的匹配中使用了非贪婪量词
+?
- 使用了对匹配的后引用<$ c $条件输入中的c> {R:1}
- removed the leading slashes in the keys in the rewrite map
- used the non-greedy quantifier
+?
in the rule's match - used a back reference to the match
{R:1}
in the condition input
我分享你在寻找体面方面遇到困难的经验文件;在以下文章的帮助下,我不得不试验一下:
I share your experience in having trouble finding decent documentation; I had to experiment my way through, with help from the following articles:
- http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite -maps-in-url-rewrite-module
- http://technet.microsoft.com/en-us/library/ee215190(v = ws.10).aspx
- http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module
- http://technet.microsoft.com/en-us/library/ee215190(v=ws.10).aspx
这篇关于在IIS7中重写映射 - 如何使匹配可选地包含尾部斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!