问题描述
我有一个IIS网站http://localhost:4200
,下面有两个文件:url.asp
和url.html
。
文件如下所示:
url.asp:
<h2>Query Strings: <%= Request.QueryString %></h2>
url.html:
<html lang="en">
<body>
<h2 id="location" />
<script type="text/javascript">
document.getElementById('location').innerText = `Query Strings: ${location.search}`;
</script>
</body>
</html>
我还有一个IIS虚拟目录http://localhost/abc
,其中有一个web.config文件,其中仅包含URL重写规则,如下所示:
web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="InboundRule1" stopProcessing="true">
<match url="z(.*)" />
<action type="Rewrite"
url="http://localhost:4200/url.asp?p={R:1}"
appendQueryString="true"
logRewrittenUrl="true" />
</rule>
<rule name="InboundRule2" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite"
url="http://localhost:4200/url.html?p={R:1}"
appendQueryString="true"
logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
<httpRedirect enabled="false" />
</system.webServer>
</configuration>
问题
当我按下http://localhost/abc/z123
时,我得到:
查询字符串:p = 123
这是我所期望的。该请求将重定向(重写)到url.asp
文件。
但是,当我按下http://localhost/abc/123
时,我得到了:
查询字符串:
在这种情况下,不打印查询字符串。该请求将重定向(重写)到url.html
文件。
因此,通过IIS URL重写,如果我将查询字符串传递给asp文件,它将获得查询字符串。但是,当我将查询字符串传递到HTML文件时,并没有得到它。
有人可以解释为什么会这样吗?
解决方法
这是因为在InboundRule2中,您的{R:1}是 http:// localhost / abc / 123 :
您需要按以下方式修改重写规则:
此时的查询字符串应为{R:2}:
<rule name="InboundRule2">
<match url="(abc)/(.*)" />
<action type="Rewrite" url="http://localhost:4200/url.html?p={R:2}" />
</rule>
更新:
这是因为URL.html将获得当前页面中URL的查询字符串。如果访问http:// localhost / abc / 123,则url.html将从此URL获取查询字符串,但是该URL中不存在查询字符串,因此您将看到Query Strings为空。
您可以尝试在http:// localhost / abc / 123之后添加查询字符串:
您可以使用重定向来解决此问题。