Skip to content

Commit d207a5e

Browse files
committed
✨ Enhance middleware to redirect non-www requests in production and update matcher configuration
1 parent f19c063 commit d207a5e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

middleware.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@ import { NextResponse } from "next/server";
22
import type { NextRequest } from "next/server";
33

44
export const config = {
5-
runtime: "experimental-edge",
5+
matcher: [
6+
/*
7+
* Match all request paths except for the ones starting with:
8+
* - api (API routes)
9+
* - _next/static (static files)
10+
* - _next/image (image optimization files)
11+
* - favicon.ico (favicon file)
12+
*/
13+
'/((?!api|_next/static|_next/image|favicon.ico).*)',
14+
],
615
};
716

817
export function middleware(request: NextRequest) {
18+
const url = request.nextUrl.clone();
19+
const hostname = request.headers.get('host') || '';
20+
const isProd = process.env.NODE_ENV !== 'development';
21+
22+
if (isProd && hostname !== 'www.mahata.org') {
23+
url.hostname = 'www.mahata.org';
24+
url.protocol = 'https';
25+
url.port = '';
26+
return NextResponse.redirect(url);
27+
}
28+
929
return NextResponse.next();
1030
}

0 commit comments

Comments
 (0)