IIS Rewrite规则条件失效问题及修复方案
IIS Rewrite规则修复:瑞典语站点重定向排除指定路径
问题场景
网站过渡阶段,需将以sv/开头的瑞典语站点路径重定向至指定信息页,但要允许rapporter及media/pressmeddelanden的所有子路径正常访问。最初配置的规则仅URL匹配功能生效,条件排除逻辑完全失效。
原失效规则
<rewrite> <rules> <clear /> <rule name="Redirect swedish site" stopProcessing="true"> <match url="sv(|\/.*)$" /> <conditions logicalGrouping="MatchAny"> <add input="{REQUEST_URI}" negate="true" pattern="rapporter(|\/.*)$" ignoreCase="true" /> <add input="{REQUEST_URI}" negate="true" pattern="media/pressmeddelanden(|\/.*)$" ignoreCase="true" /> </conditions> <action type="Redirect" url="https://www.example.com" appendQueryString="true" redirectType="Permanent" /> </rule> </rules> </rewrite>
修复后的有效规则
<rule name="Redirect swedish branch rule" stopProcessing="true"> <match url="^sv/(.*)" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_URI}" pattern="^/sv/rapporter/.*" negate="true" /> <add input="{REQUEST_URI}" pattern="^/sv/media/pressmeddelanden/.*" negate="true" /> </conditions> <action type="Redirect" url="https://www.example.com" appendQueryString="false" redirectType="Permanent" /> </rule>
核心修复说明
- 精准匹配起始路径:原匹配规则
sv(|\/.*)$未限制起始位置,可能误匹配路径中任意含sv的内容;修复后用^sv/(.*)严格匹配以sv/开头的URL路径。 - 修正条件逻辑分组:原
MatchAny搭配negate="true"会导致只要有一个排除条件不满足就触发重定向,逻辑完全颠倒;改为MatchAll后,只有当请求路径不属于任何排除路径时,才执行重定向。 - 细化排除路径匹配:原条件仅匹配路径末尾的目标字符串,修复后用
^/sv/rapporter/.*和^/sv/media/pressmeddelanden/.*精准锁定瑞典语路径下的指定子路径,避免误排除其他无关路径。 - 调整查询字符串处理:根据需求关闭了查询字符串追加(
appendQueryString="false"),避免重定向时携带原路径的冗余参数。
内容的提问来源于stack exchange,提问作者Adam7908
相关产品推荐
相关产品推荐

