Skip to content

Instantly share code, notes, and snippets.

@margauxflores
Created September 13, 2023 01:14
Show Gist options
  • Save margauxflores/636bb1a7fb288a89771d7b54b1b170bc to your computer and use it in GitHub Desktop.
Save margauxflores/636bb1a7fb288a89771d7b54b1b170bc to your computer and use it in GitHub Desktop.
Next.js Redirect All Pages to Index Page
import { NextResponse, type NextRequest } from 'next/server';
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
if (pathname == '/') {
return NextResponse.next();
}
return NextResponse.redirect(new URL('/', req.url));
}
export const config = {
matcher: '/((?!api|_next|static|public|images|favicon.ico).*)',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment