Migrate apex redirect logic from next.config.ts to middleware for improved flexibility and maintainability

This commit is contained in:
MartinBraquet
2026-04-02 12:31:11 +02:00
parent 98271784b0
commit f9a1dce7b5
2 changed files with 26 additions and 6 deletions

26
web/middleware.ts Normal file
View File

@@ -0,0 +1,26 @@
import type {NextRequest} from 'next/server'
import {NextResponse} from 'next/server'
const DOMAIN = 'compassmeet.com'
export function middleware(request: NextRequest) {
const host = request.headers.get('host') || ''
const url = request.nextUrl.clone()
// Never redirect well-known files (Android + iOS app verification)
if (url.pathname.startsWith('/.well-known/')) {
return NextResponse.next()
}
// apex → www
if (host === DOMAIN) {
url.hostname = 'www.compassmeet.com'
return NextResponse.redirect(url, 308)
}
return NextResponse.next()
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
}

View File

@@ -112,12 +112,6 @@ const nextConfig: NextConfig = {
},
async redirects() {
return [
{
source: '/((?!\\.well-known/assetlinks\\.json).*)',
has: [{type: 'host', value: 'compassmeet.com'}],
destination: 'https://www.compassmeet.com/:path*',
permanent: true,
},
{source: '/discord', destination: 'https://discord.gg/8Vd7jzqjun', permanent: false},
{source: '/patreon', destination: 'https://patreon.com/CompassMeet', permanent: false},
{