【问题标题】:IIS URL Rewrite not appending query stringIIS URL重写不附加查询字符串
【发布时间】:2021-02-04 21:26:30
【问题描述】:

我有一个 IIS 网站 http://localhost:4200,在该网站下有两个文件:url.aspurl.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>

基本上这两个文件都会在 URL 中打印查询字符串。

我还有一个 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 文件时,它没有得到它。

谁能解释一下为什么会这样?

【问题讨论】:

  • 您好,问题解决了吗?如果您认为我的回复对您有帮助,您可以将其标记为答案。

标签: html windows iis url-rewrite-module


【解决方案1】:

这是因为在 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后面加上查询字符串:

你可以使用重定向来解决这个问题。

【讨论】:

  • 很抱歉,我认为我无法让我的问题得到理解。具有 URL 重写规则的 web.config 位于名为 abc (http://localhost/abc) 的 IIS 虚拟目录下,而不是位于 http://localhost 下。 URL 重写适用于规则“InboundRule1”和“InboundRule2”,一个重定向到“.asp”文件,而另一个重定向到“.html”文件。问题是,“.asp”文件接收我通过 URL 重写规则传递的查询字符串,但“.html”文件没有。我不知道如何解释这一点并寻找解释。
  • 你删除InboundRule1还有这个问题吗?
  • 是的。我愿意。 “InboundRule1”只是为了演示在某些情况下传递查询字符串。我的问题实际上与“InboundRule2”有关。
猜你喜欢
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
相关资源
最近更新 更多