You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Server强制HTTPS配置咨询:如何将HTTP页面转为HTTPS

Windows Server 强制HTTP跳转HTTPS的实现方法

IIS环境下的主流方案(大部分Windows Server站点适用)

方法1:通过IIS URL重写模块配置

  1. 先检查URL重写模块是否安装:打开IIS管理器,在服务器节点下找“URL重写”功能,没有的话去服务器管理器添加角色功能,勾选Web服务器->应用程序开发->URL重写,完成安装。
  2. 给目标网站配置规则:
    • 选中要设置跳转的网站,打开“URL重写”
    • 右侧点击“添加规则”,选择“空白规则”
    • 规则名称填「HTTP to HTTPS Redirect」
    • 匹配URL区域:请求路径选「与模式匹配」,模式填(.*),使用正则表达式
    • 条件:点击「添加」,条件输入填{HTTPS},模式填^OFF$
    • 操作:操作类型选「重定向」,重定向URL填https://{HTTP_HOST}/{R:1},状态码选「301 永久重定向」,勾选「追加查询字符串」
    • 点击「应用」,配置生效

方法2:直接修改web.config文件

如果是ASP.NET站点或有权限修改网站根目录的web.config,直接在<system.webServer>节点内添加重写规则:

<rewrite>
  <rules>
    <rule name="HTTP to HTTPS Redirect" 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>

保存文件后IIS会自动加载配置,无需重启站点(老版本IIS可能需要手动重启站点)

其他场景补充方案

PHP站点

在站点入口文件(比如index.php)顶部加入以下代码:

if ($_SERVER['HTTPS'] != 'on') {
    $redirect_url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header('Location: ' . $redirect_url, true, 301);
    exit();
}

ASP.NET站点(代码后台实现)

在Global.asax文件的Application_BeginRequest事件中添加逻辑:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (!Context.Request.IsSecureConnection)
    {
        string redirectUrl = Context.Request.Url.ToString().Replace("http:", "https:");
        Response.RedirectPermanent(redirectUrl, true);
    }
}

内容的提问来源于stack exchange,提问作者Gaby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:01:17