IIS强制SSL重定向不会重定向根流量

问题描述

我正在使用IIS 10,并且试图将非SSL流量重定向到SSL。我已经搜索了很多,还没有找到其他特定的报告,所以我在这里问。

重定向适用于所有URL,但根流量非常奇怪。

例如,如果我输入http://sitename.com,它不会重定向https,但是,如果我输入http://sitename.com/index.html,它将重定向https

我的根页面是index.html页面(如果出于任何原因都如此)。

Web.config代码

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Force SSL" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
          <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
          <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
          <add input="{HTTP_HOST}" pattern="(www\.)?sitename\.com" />
        </conditions>
        <action type="Redirect" url="https://sitename.com{REQUEST_URI}" appendQueryString="false" />
      </rule>
      ...
    </rules>
  </rewrite>
</system.webServer>

一个要点,我已经在IIS的顶层添加了它,但是它仍然无法正常工作。

我在站点上IIS中的规则定义中注意到,它添加在输入列URL path after '\'下。

解决方法

当我测试您的规则时,“”始终匹配失败。我不确定当您匹配时结果是什么。

当我将patternSyntax =“ Wildcard”更改为正则表达式时,它起作用了。因此,我建议您使用正则表达式。

我还重写了一条新规则,供您将h​​ttp重定向到https。 Https用于确定当前请求是否使用SSL。 https的值为On和Off,并且On表示请求使用SSL。更多服务器变量可以参考this document。 即使根流量,它也始终有效。您可以使用它。

<rule name="Force SSL" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTPS}" pattern="Off" />
                    <add input="{HTTP_HOST}" pattern="sitename.com" />
    </conditions>
    <action type="Redirect" url="https://sitename.com{REQUEST_URI}" appendQueryString="false" />
  </rule>

这是失败请求跟踪中的过程。

enter image description here

enter image description here