34 lines
821 B
TypeScript
34 lines
821 B
TypeScript
import type { NextRequest } from 'next/server'
|
|
import { NextResponse } from 'next/server'
|
|
|
|
const APEX_HOST = 'greenlenspro.com'
|
|
const WWW_HOST = `www.${APEX_HOST}`
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const host = request.headers.get('host')
|
|
const forwardedProto = request.headers.get('x-forwarded-proto')
|
|
|
|
if (!host || (host !== APEX_HOST && host !== WWW_HOST)) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
const url = request.nextUrl.clone()
|
|
let shouldRedirect = false
|
|
|
|
if (host === WWW_HOST) {
|
|
url.host = APEX_HOST
|
|
shouldRedirect = true
|
|
}
|
|
|
|
if (forwardedProto === 'http' || url.protocol === 'http:') {
|
|
url.protocol = 'https:'
|
|
shouldRedirect = true
|
|
}
|
|
|
|
return shouldRedirect ? NextResponse.redirect(url, 308) : NextResponse.next()
|
|
}
|
|
|
|
export const config = {
|
|
matcher: '/:path*',
|
|
}
|