chore: business domain auto redirect to host domain

This commit is contained in:
oiov
2025-10-19 14:47:19 +08:00
parent c4a43a3a8d
commit 440d310076
2 changed files with 33 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import { NextAuthRequest } from "next-auth/lib";
import { siteConfig } from "./config/site";
import { extractRealIP, getGeolocation, getUserAgent } from "./lib/geo";
import { extractHost } from "./lib/utils";
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
@@ -12,6 +13,11 @@ export const config = {
const isVercel = process.env.VERCEL;
// 门户域名配置(只保留主机名,不包含端口、协议)
const PORTAL_DOMAIN = extractHost(
process.env.NEXT_PUBLIC_APP_URL || "localhost",
);
const redirectMap = {
"Missing[0000]": "/link-status?error=missing&slug=",
"Expired[0001]": "/link-status?error=expired&slug=",
@@ -43,6 +49,27 @@ const systemRoutes = [
"/favicon.ico",
];
// 获取主机名(不含端口)
function getHostname(hostname: string): string {
return hostname.split(":")[0].toLowerCase();
}
// 判断是否为门户域名
function isPortalDomain(hostname: string): boolean {
return getHostname(hostname) === PORTAL_DOMAIN;
}
// 判断是否为业务域名(即非门户域名)
function isBusinessDomain(hostname: string): boolean {
return !isPortalDomain(hostname);
}
// 处理业务域名的根路径请求 - 重定向到门户域名
function handleBusinessDomainRedirect(hostname: string): NextResponse {
const portalUrl = `https://${PORTAL_DOMAIN}?redirect=${hostname}`;
return NextResponse.redirect(portalUrl, 302);
}
async function handleShortUrl(req: NextAuthRequest) {
const url = new URL(req.url);
const pathname = url.pathname;
@@ -157,6 +184,11 @@ function extractSlug(url: string): string | null {
export default auth(async (req) => {
try {
const { hostname, pathname } = new URL(req.url);
// 如果是业务域名的根路径或非短链路径,重定向到门户域名
if (isBusinessDomain(hostname) && pathname === "/") {
return handleBusinessDomainRedirect(hostname);
}
return await handleShortUrl(req);
} catch (error) {
console.error("Middleware error:", error);

File diff suppressed because one or more lines are too long