设置http自动跳转https(IIS URL重写模块的配置)

参考:https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

  • 规则如下
<rewrite>
<rules>
<clear />
<rule name="redirect_https" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^test.com" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
  • 说明:

HTTP URL in this form: http(s)://<host>:<port>/<path>?<querystring>

<match>是对<path>段进行正则解析反向引用,结果放到{R:N}中
<conditions><add>中对pattern内容进行正则解析反向引用,结果放到{C:N}中

条件说明:
<add input="{HTTPS}" pattern="^OFF$" />      

=>如果是https访问 则条件无效不做跳转处理,针对http才有效
 

<add input="{HTTP_HOST}" pattern="^test.com" />

=>单独针对test.com开头的访问 才进行https自动跳转,不加这个条件则所有http访问都做https跳转


MatchAll表示必须满足所有的条件


MatchAny表示满足任一个条件即可

 

如果要针对几个别名做https跳转 则要设置多个规则:

<rewrite>
<rules>
<rule name="redirect_https_1" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^test1.cn" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
<rule name="redirect_https_2" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^test2.cn" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值