一、安装URL重写模块
1. 下载并安装URL重写扩展
2. 验证安装
二、配置HTTP到HTTPS重定向
方法一:使用URL重写规则(推荐)
1. 打开IIS管理器 → 选择目标网站
2. 双击URL重写 → 右侧点击添加规则
3. 选择空白规则
4. 按以下参数配置:
方法二:直接编辑web.config
在网站根目录的 web.config 文件中添加:
<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>
三、验证配置
方法1:浏览器验证
- 打开浏览器,输入你的网站地址,使用HTTP协议(例如: http://example.com )。
- 观察地址栏,应该会自动跳转到HTTPS( https://example.com ),并且浏览器地址栏显示安全锁标志,没有不安全警告。
方法2:使用curl命令验证
curl -I http://your_domain.com
应返回:HTTP/1.1 301 Moved Permanently