Windows Server 2012 IIS8+ 强制 HTTP 跳转 HTTPS 详细教程
技术文档
2025-08-22 23:37
758
一、配置方法(三选一)
方法1:URL重写模块(推荐)
1. 安装模块:
-
-
官方地址下载URL重写模块:https://www.iis.net/downloads/microsoft/url-rewrite
-
选择x64版本安装
-
2. 配置规则:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Force HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
方法2:IIS管理器可视化配置
1. 打开IIS管理器 → 选择目标网站
2. 双击 "URL重写" → 点击右侧 "添加规则"
3. 选择 "空白规则" 并配置:
-
-
名称:强制HTTPS
-
匹配URL:(.*)
-
条件:{HTTPS} = OFF
-
操作类型:重定向
-
重定向URL:https://{HTTP_HOST}/{R:1}
-
勾选 "301永久重定向"
-
方法3:命令行一键配置
Add-WebConfigurationProperty -PSPath "IIS:\Sites\Your_Site_Name" -Filter "system.webServer/rewrite/rules" -Name "." -Value @{
name='Force HTTPS';
stopProcessing='true';
match=@{url='(.*)'};
conditions=@{
add=@{
input='{HTTPS}';
pattern='^OFF$'
}
};
action=@{
type='Redirect';
url='https://{HTTP_HOST}/{R:1}';
redirectType='Permanent'
}
}
二、验证配置
方法1:浏览器验证
- 打开浏览器,输入你的网站地址,使用HTTP协议(例如: http://example.com )。
- 观察地址栏,应该会自动跳转到HTTPS( https://example.com ),并且浏览器地址栏显示安全锁标志,没有不安全警告。
方法2:使用curl命令验证
curl -I http://your_domain.com
应返回:HTTP/1.1 301 Moved Permanently