【问题标题】:IIS URL Rewrite module repeats query stringIIS URL 重写模块重复查询字符串
【发布时间】:2018-01-28 02:16:02
【问题描述】:

我有这条规则,将所有 HTTP 流量重定向到 HTTPS:

<rewrite>
    <rules>
        <rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" negate="false" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
        </rule>
    </rules>
</rewrite>

我在网上找到的。

它适用于没有查询字符串的简单 URL。但它重复了查询字符串。例如http://example.com/path?key=value 变为https://example.com/path?key=value&amp;key=value

这会导致调试和故障排除出现问题。是什么导致了这种行为?

【问题讨论】:

    标签: url-rewrite-module


    【解决方案1】:

    问题是变量 REQUEST_URI 包含 URL 和查询字符串。在您的情况下,您有两种解决方案:

    1) 将appendQueryString="false" 属性添加到您的操作中。规则应该是这样的:

    .

    <rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" negate="false" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Found" />
    </rule>
    

    2) 你可以使用URL 变量代替REQUEST_URI,因为这个变量只包含没有查询字符串的URL。规则应该是这样的:

    .

    <rule name="RedirectToHttps" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" negate="false" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="Found" />
    </rule>
    

    【讨论】:

    • 值得注意的是,我认为此页面上的官方 MS 文档是错误的:docs.microsoft.com/en-us/iis/web-dev-reference/server-variables 它指出 REQUEST_URI 是“URI 的绝对路径部分。例如 https://contoso.com:8042/over/there?name=ferret 将返回/over/there" 这个答案是正确的。 REQUEST_URI 包含查询字符串。
    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多