How do I redirect using middleware?

I have a /signup page and I'm trying to get all client requests to redirect to that page. I've tried using next config and vercel without much luck, now trying with middleware Middleware looks like this: import { NextResponse } from 'next/server' import type { NextRequest } from 'next/server' export function middleware(request: NextRequest) { return NextResponse.redirect(new URL('/signup', request.url)) } export const config = { matcher: [ '/((?!signup|api|_next/static|_next/image|favicon.ico).*)', ], } This too, doesn't work. Located in the src folder. What am I missing here?
1 Reply
FullerPrime
FullerPrimeOP15mo ago
Ok so this works: export function middleware(request: NextRequest) {
const url = request.nextUrl.clone() if (!url.pathname.startsWith('/signup') && // exlude the signup page to avoid redirect loop !url.pathname.startsWith('/_next') && // exclude Next.js internals !url.pathname.startsWith('/_axiom') && // exclude Axiom !url.pathname.startsWith('/_api') && // exclude all API routes !url.pathname.includes('.')) // exclude public files { url.pathname = '/signup' return NextResponse.redirect(url)
} } I've not been able to get this working using regex matches which I think will probably be the correct way to do things Any pointers?
Want results from more Discord servers?
Add your server