[英]IIS URL Rewrite not appending query string
我有一个 IIS 网站http://localhost:4200
在该网站下我有两个文件: url.asp
和url.html
。
这些文件看起来像这样:
网址.asp:
<h2>Query Strings: <%= Request.QueryString %></h2>
网址.html:
<html lang="en">
<body>
<h2 id="location" />
<script type="text/javascript">
document.getElementById('location').innerText = `Query Strings: ${location.search}`;
</script>
</body>
</html>
本质上,这两个文件都在 URL 中打印查询字符串。
我还有一个 IIS 虚拟目录http://localhost/abc
,其中我有一个 web.config 文件,其中仅包含 URL 重写规则,如下所示:
网络配置:
<?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 之后添加查询字符串:
您可以使用重定向来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.