IIS 10 URL 将 http 流量重写为 https 重定向不起作用

IIS 10 URL Rewrite http traffic to https redirect not working

当客户端请求 http 而不是 https 时,AWS 应用程序负载均衡器后面的 IIS 10 服务器不会为没有 www 的域重定向流量。指定 www 时重定向流量的规则工作正常,但如果您在没有 www.

的情况下尝试相同的 url,则会返回 404

所以:

  1. 输入“http://dname.com/blog”= 404

  2. 输入“http://www.dname.com/blog”=重定向到“https://www.dname.com/blog”

            <rule name="Force HTTPS" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^dname\.com$" />
                </conditions>
                <action type="Rewrite" url="https://www.dname.com{REQUEST_URI}" />
            </rule>
            <rule name="Force WWW HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^www\.dname\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
            </rule>
    

我不知道为什么以前发布的规则不起作用,但我能够创建一个有效的改进规则:

            <rule name="Force HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^(www\.)?dname\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
            </rule>

以上规则结合了这两个规则,而不是在单独的规则中查找不带 www 的域,然后再查找带 www 的域。正则表达式 (www\.) 告诉规则查找“www”。问号告诉它它可能存在也可能不存在,因此包括带 www.

和不带 www.

的域

在设置重定向配置之前,有一个非常非常重要的步骤需要注意。

在 web Sites project --> Actions(in the right) --> Bindings 中,内容如下: Binding Content

你仔细看黄色部分,黄色部分是你原来的网址。这个原始 IP 地址必须存在于“站点绑定”中,如果没有黄色部分,URL 重定向将不再起作用

以下配置是我当前的 IIS URL 重定向设置:

    <rewrite>
        <globalRules>
            <rule name="Http redirect to Https" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern="localhost:8080" />   <-- the Red one should match above Site Bindings original IP address
                </conditions>
                <action type="Redirect" url="https://Your-Host-Name/{R:1}" redirectType="SeeOther" />
            </rule>
        </globalRules>
    </rewrite>

即使在浏览了不同论坛上提供的答案后,也没有任何效果。 经过 2 天的努力,我发现解决了这个问题:

  1. 绑定:必须启用端口 80(这可以添加到 IIS 的绑定部分)。

  2. SSL 设置:必须取消选中所需的 SSL。

  3. 添加规则:

<rewrite>
    <rules>
        <rule name="http to https redirection" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
                appendQueryString="false" />
        </rule>
    </rules>
</rewrite>
  1. 验证网络配置,因为它应该反映在 IIS 中添加的规则。