我发布这篇文章更多是为了回答标题中的问题,因为我们在与Cloudflare的帮助支持团队沟通后已经解决了这个问题。由于在进行研究时,我们找不到解决方案,这可能会帮助其他人!
如果启用Cloudflare的Flexible SSL,并且您希望使用如How to force HTTPS using a web.config file中所述的方法将所有请求的http
重定向到https
,则很可能会出现重定向循环。
您可以选择通过CloudFlare的页面规则使用页面规则,如中所述https://support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-all-visitors-to-HTTPS-SSL-但在我们的案例中,这仍然会在网站中留下一些非https请求,从而使网站变得不安全。
在同一篇Cloudflare支持文章中可以看到https://support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-all-visitors-to-HTTPS-SSL-,Cloudflare会附加CF Visitor HTTP标头,如下所述:
CF-Visitor: {"scheme":"https"}
因此,适用于我们的web.config <rewrite>
片段如下:
<rewrite>
<rules>
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_CF_VISITOR}" pattern="(.*)https(.*)" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>