[英]IIS URL Rewrite not appending query string
I have a an IIS website http://localhost:4200
under which I have two files: url.asp
and url.html
.我有一个 IIS 网站
http://localhost:4200
在该网站下我有两个文件: url.asp
和url.html
。
The files look like so:这些文件看起来像这样:
url.asp:网址.asp:
<h2>Query Strings: <%= Request.QueryString %></h2>
url.html:网址.html:
<html lang="en">
<body>
<h2 id="location" />
<script type="text/javascript">
document.getElementById('location').innerText = `Query Strings: ${location.search}`;
</script>
</body>
</html>
Essentially both files print the query strings in the URL.本质上,这两个文件都在 URL 中打印查询字符串。
I also have an IIS virtual directory http://localhost/abc
where I have a single web.config file which contains only URL rewrite rules and looks like so:我还有一个 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>
Question题
When I hit http://localhost/abc/z123
I get:当我点击
http://localhost/abc/z123
我得到:
Query Strings: p=123
查询字符串:p=123
This is what I am expecting.这是我所期待的。 This request gets redirected (rewritten) to the
url.asp
file.此请求被重定向(重写)到
url.asp
文件。
However, when I hit http://localhost/abc/123
I get:但是,当我点击
http://localhost/abc/123
我得到:
Query Strings:
查询字符串:
No query string is printed in this case.在这种情况下不打印查询字符串。 This request gets redirected (rewritten) to the
url.html
file.此请求被重定向(重写)到
url.html
文件。
So, via IIS URL rewrite, if I pass a query string to an asp file it gets the query string.因此,通过 IIS URL 重写,如果我将查询字符串传递给 asp 文件,它会获取查询字符串。 But when I pass the query string to an HTML file it does not get it.
但是当我将查询字符串传递给 HTML 文件时,它没有得到它。
Can someone please explain why this happens?有人可以解释为什么会发生这种情况吗?
This is because in InboundRule2, your {R:1} is http://localhost/abc/123 :这是因为在 InboundRule2 中,您的 {R:1} 是http://localhost/abc/123 :
You need to modify your rewrite rules as follows:你需要修改你的重写规则如下:
The query string at this time should be {R:2}:此时的查询字符串应该是{R:2}:
<rule name="InboundRule2">
<match url="(abc)/(.*)" />
<action type="Rewrite" url="http://localhost:4200/url.html?p={R:2}" />
</rule>
UPDATE:更新:
This is because URL.html will get the query string of the URL in the current page.这是因为 URL.html 会获取当前页面中 URL 的查询字符串。 If you visit http://localhost/abc/123, url.html will get the query string from this URL, but the query string does not exist in this URL, so you will see that Query Strings is empty.
如果你访问http://localhost/abc/123,url.html会从这个URL获取查询字符串,但是这个URL中不存在查询字符串,所以你会看到Query Strings是空的。
You can try to add the query string after http://localhost/abc/123:您可以尝试在 http://localhost/abc/123 之后添加查询字符串:
You can use redirect to solve this problem.您可以使用重定向来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.