参考: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>