You need to enable JavaScript to run this app.
文档中心
内容分发网络

内容分发网络

复制全文
下载 pdf
示例代码
将HTTP重定向到HTTPS
复制全文
下载 pdf
将HTTP重定向到HTTPS

该示例展示了如何将用户请求使用的 HTTP 协议重定向为 HTTPS 协议。

addEventListener("fetch", (event) => {
  event.respondWith(redirectToHTTPS(event.request));
});

async function redirectToHTTPS(request) {
  const url = new URL(request.url);

  if (url.protocol === "http:") {
    url.protocol = "https:";

    return new Response(null, {
      status: 301,
      headers: {
        Location: url.toString(),
      },
    });
  }

  return fetch(request);
}

最近更新时间:2026.10.08 16:07:32
这个页面对您有帮助吗?
有用
有用
无用
无用