Skip to content

Windows IIS 强制 HTTPS

准备工作

  • 确保已安装 IIS URL Rewrite Module
  • SSL 证书已正确安装
  • 验证证书状态正常

配置步骤

  1. 安装 URL Rewrite 模块

    下载并安装 IIS URL Rewrite Module 2008系统点击下载2012以上版本系统点击下载

  2. 配置重定向规则

    打开站点根目录下的 web.config 文件(如果没有则新建一个)

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
       <system.webServer>
          <rewrite>
             <rules>
                <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                   <match url="(.*)" />
                   <conditions>
                      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                   </conditions>
                   <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
                </rule>
             </rules>
          </rewrite>
       </system.webServer>
    </configuration>

验证配置

  • 使用 HTTP 访问网站
  • 确认是否自动跳转到 HTTPS
  • 检查跳转是否正常

常见问题

  1. 跳转不生效

    • 检查 URL 重写模块是否正确安装
    • 验证重定向规则是否正确
    • 确认 IIS 服务是否重启
  2. 循环跳转

    • 检查 SSL 证书配置
    • 验证重定向规则
    • 查看服务器错误日志

SSL 证书帮助中心