33 lines
949 B
TypeScript
33 lines
949 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
/** 生产域名:微信内访问无协议或 http 时常异常,统一 308 到 HTTPS */
|
|
const FORCE_HTTPS_HOSTS = new Set(["vip.hackrobot.cn", "www.vip.hackrobot.cn"]);
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const rawHost = request.headers.get("host");
|
|
const host = rawHost?.split(":")[0]?.toLowerCase() ?? "";
|
|
if (!host || !FORCE_HTTPS_HOSTS.has(host)) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const forwarded = request.headers.get("x-forwarded-proto");
|
|
const proto = forwarded?.split(",")[0]?.trim().toLowerCase();
|
|
const url = request.nextUrl.clone();
|
|
|
|
const isHttp =
|
|
proto === "http" ||
|
|
(proto === undefined && url.protocol === "http:");
|
|
|
|
if (isHttp) {
|
|
url.protocol = "https:";
|
|
return NextResponse.redirect(url, 308);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/:path*"],
|
|
};
|