import { NextResponse } from 'next/server' const middleware = (request) => { if (request.nextUrl.pathname === request.nextUrl.pathname.toLocaleLowerCase()) return NextResponse.next() return NextResponse.redirect( `${request.nextUrl.origin}${request.nextUrl.pathname.toLocaleLowerCase()}` ) } export { middleware }
(Next v.12.2.3) No matter what I code, pages/middleware.js seems not running at all!
I wanna redirect uppercase urls to lowercase urls.
what should I do?
Advertisement
Answer
Create a
middleware.ts
(or.js
) file at the same level as yourpages
directory
So it shouldn’t be inside pages/
, but in the same directory as pages/
.