-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
27 lines (22 loc) · 753 Bytes
/
middleware.js
File metadata and controls
27 lines (22 loc) · 753 Bytes
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
import { next } from '@vercel/edge'
export default function middleware(request) {
const url = new URL(request.url)
// Trailing slash redirect (but preserve root /)
if (url.pathname.endsWith('/') && url.pathname !== '/') {
url.pathname = url.pathname.replace(/\/+$/, '')
return Response.redirect(url.toString(), 308)
}
// Mixed-case /universe/ redirect to lowercase
const universeMatch = url.pathname.match(/^\/universe\/([a-z0-9-]+)$/i)
if (universeMatch) {
const lower = universeMatch[1].toLowerCase()
if (universeMatch[1] !== lower) {
url.pathname = `/universe/${lower}`
return Response.redirect(url.toString(), 301)
}
}
return next()
}
export const config = {
matcher: ['/universe/:path*'],
}