FullerPrime
TTCTheo's Typesafe Cult
•Created by FullerPrime on 9/30/2023 in #questions
How do I redirect using middleware?
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?
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?
3 replies