IIS重定向-服务器变量值不正确

问题描述 投票:0回答:1

我试图为我的网站强制使用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>
  1. 情况1:转到http://demo.com => https://www.demo.com?c0=off
  2. 情况2:转到https://demo.com => https://www.demo.com?c0=off
  3. 情况3:转到http://www.demo.com => http://demo.com(无重定向)
asp.net redirect iis url-rewriting web-config
1个回答
0
投票

我发现我的网站使用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>
© www.soinside.com 2019 - 2024. All rights reserved.