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

修改web.config重写规则:移除index.asp但保留同名后缀文件

修正URL重写规则避免误匹配非index.asp文件

我在web.config中配置了如下URL重写规则:

<!-- Rule to remove "index.asp" -->
<rule name="Default document rewrite" stopProcessing="true">
    <match url="(.*)index.asp" />
    <action type="Redirect" url="{R:1}" redirectType="Temporary" />
</rule>

(确认无误后我会将redirectType从"Temporary"改为"Permanent")

当前规则能正常处理:

  • sitename.com/index.asp → sitename.com
  • sitename.com/dir/index.asp → sitename.com/dir/

但访问sitename.com/testindex.asp时,会被错误重定向为sitename.com/test,这个URL应该保持不变。需要修改规则,同时保留原URL的查询字符串。


修改后的规则

<!-- Rule to remove "index.asp" -->
<rule name="Default document rewrite" stopProcessing="true">
    <match url="(.*\/)?index\.asp$" />
    <action type="Redirect" url="{R:1}" redirectType="Temporary" appendQueryString="true" />
</rule>

关键调整说明

  • (.*\/)?:匹配可选的路径前缀,确保index.asp的前置字符要么是路径分隔符/,要么处于URL根位置
  • index\.asp$:用$锚定到URL末尾,确保只匹配以index.asp结尾的URL;同时转义.,避免它匹配任意字符
  • appendQueryString="true":自动保留原URL的查询字符串,比如sitename.com/index.asp?id=2会被重定向为sitename.com/?id=2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 12:48:40