内容分发网络
该示例展示了如何将用户请求使用的 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); }