feat: add middleware to redirect http to https
This commit is contained in:
28
middleware.ts
Normal file
28
middleware.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { NextRequest } from 'next/server';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
if (
|
||||
process.env.NODE_ENV === 'production' &&
|
||||
!request.headers.get('x-forwarded-proto')?.startsWith('https')
|
||||
) {
|
||||
return NextResponse.redirect(
|
||||
`https://${request.headers.get('host')}${request.nextUrl.pathname}`,
|
||||
301
|
||||
);
|
||||
}
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
/*
|
||||
* Match all request paths except for the ones starting with:
|
||||
* - api (API routes)
|
||||
* - _next/static (static files)
|
||||
* - _next/image (image optimization files)
|
||||
* - favicon.ico (favicon file)
|
||||
*/
|
||||
'/((?!api|_next/static|_next/image|favicon.ico).*)'
|
||||
]
|
||||
};
|
||||
Reference in New Issue
Block a user