Skip to content

Commit 38c11d0

Browse files
ralyodioclaude
andcommitted
feat: add middleware to redirect www and railway.app to canonical domain
301 redirects for: - www.c0upons.comhttps://c0upons.com - *.railway.app / *.up.railway.app → https://c0upons.com - http → https Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f98c73a commit 38c11d0

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

apps/web/middleware.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { NextRequest, NextResponse } from 'next/server';
2+
3+
const CANONICAL = 'https://c0upons.com';
4+
5+
export function middleware(req: NextRequest) {
6+
const host = req.headers.get('host') ?? '';
7+
const proto = req.headers.get('x-forwarded-proto') ?? 'https';
8+
9+
// Strip www
10+
const isWww = host.startsWith('www.');
11+
// Redirect railway temp domain to canonical
12+
const isRailway = host.endsWith('.railway.app') || host.endsWith('.up.railway.app');
13+
// Redirect plain http to https (in case Railway forwards it)
14+
const isHttp = proto === 'http';
15+
16+
if (isWww || isRailway || isHttp) {
17+
const url = req.nextUrl.clone();
18+
url.protocol = 'https';
19+
url.host = 'c0upons.com';
20+
url.port = '';
21+
return NextResponse.redirect(url, { status: 301 });
22+
}
23+
24+
return NextResponse.next();
25+
}
26+
27+
export const config = {
28+
matcher: [
29+
'/((?!_next/static|_next/image|favicon\\.svg|favicon\\.ico|.*\\.png|.*\\.svg|.*\\.jpg|.*\\.webp).*)',
30+
],
31+
};

0 commit comments

Comments
 (0)