-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
36 lines (30 loc) · 1.04 KB
/
middleware.ts
File metadata and controls
36 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Skip for static files and api routes
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api') ||
pathname.includes('.') // files like favicon.png, sitemap.xml
) {
return NextResponse.next()
}
// Redirect /kr to / (301 permanent redirect for SEO)
if (pathname === '/kr' || pathname.startsWith('/kr/')) {
const url = request.nextUrl.clone()
url.pathname = pathname.replace(/^\/kr/, '') || '/'
return NextResponse.redirect(url, 301)
}
// /en routes - pass through to [locale] handler
if (pathname === '/en' || pathname.startsWith('/en/')) {
return NextResponse.next()
}
// Root and other paths - rewrite to /kr internally (no redirect)
const url = request.nextUrl.clone()
url.pathname = `/kr${pathname}`
return NextResponse.rewrite(url)
}
export const config = {
matcher: ['/((?!_next|api|.*\\..*).*)'],
}