@@ -3,18 +3,44 @@ import createMiddleware from "next-intl/middleware"
33
44import { routing } from "./src/i18n/routing"
55import { DEFAULT_LOCALE } from "./src/lib/constants"
6+ import { getFirstSegment } from "./src/lib/utils/url"
67
78const handleI18nRouting = createMiddleware ( routing )
89
10+ // Locales that have been removed but may have external links pointing to them
11+ const DEPRECATED_LOCALES = new Set ( [ "pcm" , "fil" , "ph" ] )
12+
13+ // Legacy locale codes that should redirect to their current equivalents
14+ const LOCALE_ALIASES : Record < string , string > = { no : "nb" }
15+
16+ function redirectTo ( request : NextRequest , pathname : string , status : number ) {
17+ const url = request . nextUrl . clone ( )
18+ url . pathname = pathname
19+ return NextResponse . redirect ( url , status )
20+ }
21+
922export default function middleware ( request : NextRequest ) {
10- // Normalize to lowercase paths site-wide (URLs are case-insensitive by spec,
11- // but our routes are defined in lowercase). Do this BEFORE i18n routing.
12- const originalPath = request . nextUrl . pathname
13- const lowerPath = originalPath . toLowerCase ( )
14- if ( originalPath !== lowerPath ) {
15- const url = request . nextUrl . clone ( )
16- url . pathname = lowerPath
17- return NextResponse . redirect ( url , 301 )
23+ const { pathname } = request . nextUrl
24+
25+ const lowerPath = pathname . toLowerCase ( )
26+ if ( pathname !== lowerPath ) {
27+ return redirectTo ( request , lowerPath , 301 )
28+ }
29+
30+ const firstSegment = getFirstSegment ( lowerPath )
31+
32+ if ( firstSegment && DEPRECATED_LOCALES . has ( firstSegment ) ) {
33+ // Strip deprecated locale and redirect to default locale version
34+ const rest = lowerPath . slice ( firstSegment . length + 1 )
35+ const newPath = ! rest ? "/" : rest
36+ return redirectTo ( request , newPath , 302 )
37+ }
38+
39+ if ( firstSegment && firstSegment in LOCALE_ALIASES ) {
40+ // Replace legacy locale code with current one
41+ const newLocale = LOCALE_ALIASES [ firstSegment ]
42+ const newPath = `/${ newLocale } ${ lowerPath . slice ( firstSegment . length + 1 ) } `
43+ return redirectTo ( request , newPath , 301 )
1844 }
1945
2046 // Handle i18n routing
0 commit comments