如何解决web.config中location路径携带?id参数的重定向报错问题?
解决web.config中带查询参数的重定向问题
你猜的没错,<location>节点的path属性只能匹配URL的路径部分,不包含?之后的查询字符串,所以你写的name-page.asp?id=1根本匹配不到实际请求,导致重定向失效。要处理带查询参数的重定向,得用IIS的URL Rewrite模块来实现,具体配置如下:
直接在web.config的<system.webServer>节点下添加以下规则:
<rewrite> <rules> <rule name="Redirect name-page with id=1" stopProcessing="true"> <match url="^name-page\.asp$" ignoreCase="true" /> <conditions> <add input="{QUERY_STRING}" pattern="^id=1$" ignoreCase="true" /> </conditions> <action type="Redirect" url="new-page.asp" redirectType="Permanent" /> </rule> </rules> </rewrite>
配置说明:
<match url="^name-page\.asp$" />:匹配路径为name-page.asp的请求,ignoreCase="true"忽略大小写(兼容Name-Page.asp这类大小写不一致的请求)<conditions>里的规则:匹配查询字符串为id=1的请求,同样忽略大小写,能兼容你提到的?Id=1(大写I)的情况<action>:设置重定向目标为new-page.asp,redirectType="Permanent"对应301永久重定向,和你原来的httpResponseStatus="Permanent"效果一致
把这段配置加到你的web.config里,替换掉原来的<location>节点配置,就能实现目标重定向了。
内容的提问来源于stack exchange,提问作者Lello
相关产品推荐
相关产品推荐

