You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让IIS RewriteMap忽略所有查询字符串实现重定向?

解决IIS重定向映射忽略查询字符串问题

我们为客户搭建了新网站,已将旧网站的站点地图导入IIS的rewritemap文件。目前遇到的问题是:旧网站URL包含大量查询字符串(例如/projects/project1?page=2),但新网站不使用这些参数。需要实现忽略所有查询字符串,当去除查询字符串后的源URL在rewritemap中存在时,执行重定向——比如访问/projects/project1?page=2时,匹配rewritemap中的/projects/project1/并跳转。

当前使用的重写配置如下:

<rewrite>
  <rewriteMaps configSource="rewritemaps.config"></rewriteMaps>
  <rules>
    <rule name="Redirect rule1 for Redirects">
      <match url="(.*[^/])" />
      <conditions>
        <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

解决方案

核心思路是用不含查询字符串的路径变量匹配rewritemap,同时兼容路径末尾带/和不带/的情况,修改后的配置如下:

<rewrite>
  <rewriteMaps configSource="rewritemaps.config"></rewriteMaps>
  <rules>
    <rule name="Redirect non-trailing slash paths" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{Redirects:{PATH_INFO}/}" pattern="(.+)" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
    </rule>
    <rule name="Redirect trailing slash paths" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{Redirects:{PATH_INFO}}" pattern="(.+)" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

关键改动说明

  • 用{PATH_INFO}替代{REQUEST_URI}:{PATH_INFO}仅包含URL的路径部分,自动忽略查询字符串,完美匹配rewritemap中的无参数路径
  • 分规则处理尾斜杠:分别匹配末尾带/和不带/的路径,确保rewritemap中无论哪种格式的键都能被命中
  • 添加stopProcessing="true":每条规则匹配后停止后续规则处理,避免冗余执行
  • 保留appendQueryString="false":确保重定向后的URL不会附带原查询字符串

内容的提问来源于stack exchange,提问作者Cryothic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 07:00:11