HTTP 到 HTTPS 重定向 - IIS 8.5 无法正常工作

HTTP to HTTPS Redirect - IIS 8.5 not working properly

我在此处以及网上(IIS 博客等)上阅读了很多帖子。我正在尝试 强制所有从 domain.com 到 www.domain.com 的连接,同时强制从 HTTP 到 HTTPS 的请求.

我正在使用这组规则并重写,但唯一发生的事情是它重定向正常但没有重定向到 SSL。

<!-- Redirect to HTTPS -->
<rewrite>
    <rules>
        <rule name="Redirect to www" stopProcessing="true">
            <match url="(.*)" />
            <conditions trackAllCaptures="false">
                <add input="{HTTP_HOST}" matchType="Pattern" pattern="^mydomain.com$" ignoreCase="true" negate="false" />
            </conditions>
            <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.mydomain.com/{R:1}" />
        </rule>
    </rules>
    <rewriteMaps>
        <rewriteMap name="MapProtocol" defaultValue="http">
          <add key="on" value="https" />
          <add key="off" value="http" />
        </rewriteMap>
    </rewriteMaps>
</rewrite>

我做错了什么?

主要博客参考:http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action and this SO post - web.config redirect non-www to www

编辑:所以我找到了这个博客 post:http://www.meltedbutter.net/wp/?p=231 并尝试了一下,瞧!工作起来很有魅力。不知道为什么这对上面的规则 posted 有效,但在我的情况下,下面的规则有效并成功地获取所有非 www 流量并将其重定向到 wwwhttps.

<!-- Redirect to HTTPS -->
        <rewrite>
            <rules>
                <rule name="http to https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://www.domain.com/{R:1}" redirectType="SeeOther" />
                </rule>
            </rules>
        </rewrite>

我们在使用 URL 重写模块从 http 重定向到 https 时遇到了同样的问题,但是在关闭 IIS 中的 require ssl 模块后就成功了。

  1. 转到网站的 SSL 设置
  2. 取消选中需要 SSL 复选框。
  3. 应用设置并重启 iis

在某些情况下这是一个远景,但我已经删除了网站的端口 80 绑定,因为我只想要 SSL/端口 443。所以据我所知,重写也需要端口 80 绑定才能正常工作。