IIS 配置web.config 实现http重定向跳转HTTPS,包括不带www跳转到带www?

Maha ·
更新时间:2024-09-20
· 988 次阅读

为了保证域名统一,将访问 http://www.mscto.com、http://mscto.com、https://www.mscto.com 的域名都跳转到 https://mscto.com,IIS 可以进行如下配置 (需要安装 IIS UrlRewrite 模块,代码注释是为了方便理解,部署到线上请删除中文注释):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HostNameRule1">
<match url="(.*)" />
<!--匹配所有条件-->
<conditions logicalGrouping="MatchAny">
<!--当不是使用https协议访问的时候-->
<add input="{HTTPS}" pattern="^OFF$" />
<!--并且访问的host不是xiuzhanyun.com这种,例如www.xiuzhanyun.com-->
<add input="{HTTP_HOST}" pattern="^xiuzhanyun\.com$" negate="true" />
</conditions>
<!--跳转到https-->
<action type="Redirect" url="https://xiuzhanyun.com/{R:1}" />
</rule>
<rule name="HTTPS redirect">
<match url="(.*)" />
<conditions>
<!--当使用HTTPS协议访问-->
<add input="{HTTPS}" pattern="^ON$" />
<!--当访问 https://www.xiuzhanyun.com的时候 -->
<add input="{HTTP_HOST}" pattern="^xiuzhanyun\.com$" negate="true" />
</conditions>
<!--跳转到HTTPS-->
<action type="Redirect" url="https://xiuzhanyun.com/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
这里需要注意,想让 https://www.mscto.com 也可以跳转到 https://mscto.com,在申请 HTTPS 证书的时候,要把 www.mscto.com 这种域名也给申请上,否则浏览器会解析不出 https://www.mscto.com,因为在进行 HTTPS 加密握手的时候就会认证失败。
chrome 调试中发现 HTTPS 改动不生效
HTTPS 第一次连接域名的时候会和证书颁发机构进行 HTTPS 证书认证,后续的连接会缓存起来,清缓存就好了。



web.config https www Web HTTP IIS config

需要 登录 后方可回复, 如果你还没有账号请 注册新账号