修改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.comsitename.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
相关产品推荐
相关产品推荐

