我试图为我的网站强制使用SSL。但是我对{HTTPS}变量有问题,即使我通过https进入网站,它也始终返回“ OFF”。这是我的代码
<rule name="Demo" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
<add input="{HTTP_HOST}" pattern="^demo\.com$" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://www.demo.com/{R:0}?c0={C:0}" />
</rule>
我发现我的网站使用Web Farm(负载均衡),因此我必须使用{HTTP_X_FORWARDED_PROTO}而不是{HTTPS}来检测请求。这是我的代码,希望对其他人有用
<rule name="Force WWW and HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?demo\.com$" />
</conditions>
<action type="Redirect" url="https://www.demo.com/{R:0}" />
</rule>
<rule name="Force WWW on https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^https$" />
<add input="{HTTP_HOST}" pattern="^demo.com$" />
</conditions>
<action type="Redirect" url="https://www.demo.com/{R:0}" />
</rule>