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

.NET应用web.config URL重写规则需求:路径与查询参数修改

ASP.NET Web.config URL重写规则:路径替换并保留查询参数

以下是针对需求生成的URL重定向规则,可直接添加到.NET应用的web.config文件中,依赖IIS URL重写模块(需确保服务器已安装该模块):

方案1:严格匹配指定查询参数

适用于需要确保URL包含ID、FormTemplateID、FormItemID三个参数的场景:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Redirect QuickLinks Forms to Services" stopProcessing="true">
        <!-- 匹配路径部分(不含域名和查询参数) -->
        <match url="^quickLinks/forms/formdetail$" />
        <!-- 捕获查询参数中的三个值 -->
        <conditions>
          <add input="{QUERY_STRING}" pattern="ID=([^&]+)&FormTemplateID=([^&]+)&FormItemID=([^&]+)" />
        </conditions>
        <!-- 重定向到新路径,传递捕获的参数值 -->
        <action type="Redirect" url="services/forms/formdetail?ID={C:1}&FormTemplateID={C:2}&FormItemID={C:3}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

方案2:灵活传递所有查询参数

如果不需要严格限定查询参数的数量或顺序,仅需替换路径并保留所有原有查询参数,可使用更简洁的规则:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Redirect QuickLinks Forms to Services" stopProcessing="true">
        <match url="^quickLinks/forms/formdetail$" />
        <!-- 直接传递完整查询字符串 -->
        <action type="Redirect" url="services/forms/formdetail?{QUERY_STRING}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

关键参数说明

  • redirectType="Permanent":设置为301永久重定向,利于SEO;若需临时重定向,可改为Temporary(302)。
  • stopProcessing="true":匹配到该规则后停止处理后续规则,避免冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 11:05:14