web.config 的哪个正则表达式将匹配 URL 的一部分并允许此文本之后的任何内容?
What regex for a web.config will match part of a URL and allow anything after this text?
我正在努力完成 IIS 7 中 web.config 文件的正则表达式。我们有数百个页面将被重定向(根据客户请求)。其中许多页面都有 URL 类似这样的模式:
- http://site/aaa-bbb/different1.html
- http://site/aaa-bbb/different2.html
- http://site/aaa-bbb/different-again/and-so-on.html
我们打算将这些请求重定向到一个公共页面,如下所示:
我需要考虑十几种这样的 URL 模式。所以我只是想让这个重写匹配起作用,而不是将他们的所有页面作为键值对应用到重写映射中。这是我目前所做的,您可以看到我已经注释掉的一些尝试。
<rewrite>
<rewriteMaps>
<rewriteMap name="301Redirects">
<add key="/index.htm" value="/" />
<add key="/index.html" value="/" />
<add key="/customPage.htm" value="/" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="301 Redirect Rule">
<match url=".*" />
<conditions>
<add input="{301Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" />
</rule>
<!-- Here is the rule I can't get to work. -->
<rule name="301 to aaa/bbb" stopProcessing="true">
<match url="^/aaa-bbb([\d\w\s])" />
<!--<match url="^/aaa-bbb(.{0,1})" />-->
<!--<match url="^/aaa-bbb(.?)" />-->
<!--<match url="^/aaa-bbb(.*)$" />-->
<!--<match url="^/aaa-bbb(\s\S)" />-->
<!--<match url="^/aaa-bbb[\s\S]*/([\s\S]*?(.htm|.html))" />-->
<!--<match url="^/aaa-bbb(.)" />-->
<!--<match url="^/aaa-bbb(.*)" />-->
<!--<match url="^/aaa-bbb([ _0-9a-z-]+)" />-->
<action type="Redirect" url="/aaa/bbb" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
现在,如果我输入以下 URL。
这将成功重定向到此:
但是在 aaa-bbb 的末尾附加任何其他内容最终会产生 404 错误。感谢您的帮助。
问题是您使用 /
开始了您的正则表达式。在 <match url=
中,重写模块会将带有起始斜杠的路径(例如:aaa-bbb/different1.html
)与您的正则表达式进行比较。工作示例是:
<rule name="301 to aaa/bbb" stopProcessing="true">
<match url="^aaa-bbb(.?)" />
<action type="Redirect" url="/aaa/bbb" redirectType="Permanent" />
</rule>
我正在努力完成 IIS 7 中 web.config 文件的正则表达式。我们有数百个页面将被重定向(根据客户请求)。其中许多页面都有 URL 类似这样的模式:
- http://site/aaa-bbb/different1.html
- http://site/aaa-bbb/different2.html
- http://site/aaa-bbb/different-again/and-so-on.html
我们打算将这些请求重定向到一个公共页面,如下所示:
我需要考虑十几种这样的 URL 模式。所以我只是想让这个重写匹配起作用,而不是将他们的所有页面作为键值对应用到重写映射中。这是我目前所做的,您可以看到我已经注释掉的一些尝试。
<rewrite>
<rewriteMaps>
<rewriteMap name="301Redirects">
<add key="/index.htm" value="/" />
<add key="/index.html" value="/" />
<add key="/customPage.htm" value="/" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="301 Redirect Rule">
<match url=".*" />
<conditions>
<add input="{301Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" />
</rule>
<!-- Here is the rule I can't get to work. -->
<rule name="301 to aaa/bbb" stopProcessing="true">
<match url="^/aaa-bbb([\d\w\s])" />
<!--<match url="^/aaa-bbb(.{0,1})" />-->
<!--<match url="^/aaa-bbb(.?)" />-->
<!--<match url="^/aaa-bbb(.*)$" />-->
<!--<match url="^/aaa-bbb(\s\S)" />-->
<!--<match url="^/aaa-bbb[\s\S]*/([\s\S]*?(.htm|.html))" />-->
<!--<match url="^/aaa-bbb(.)" />-->
<!--<match url="^/aaa-bbb(.*)" />-->
<!--<match url="^/aaa-bbb([ _0-9a-z-]+)" />-->
<action type="Redirect" url="/aaa/bbb" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
现在,如果我输入以下 URL。
这将成功重定向到此:
但是在 aaa-bbb 的末尾附加任何其他内容最终会产生 404 错误。感谢您的帮助。
问题是您使用 /
开始了您的正则表达式。在 <match url=
中,重写模块会将带有起始斜杠的路径(例如:aaa-bbb/different1.html
)与您的正则表达式进行比较。工作示例是:
<rule name="301 to aaa/bbb" stopProcessing="true">
<match url="^aaa-bbb(.?)" />
<action type="Redirect" url="/aaa/bbb" redirectType="Permanent" />
</rule>