Skip to content

Commit

Permalink
[Common] [Hotfix] 미들웨어 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
suwonthugger committed Nov 13, 2024
1 parent ce2d8c7 commit c682e49
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { NextRequest, NextResponse } from 'next/server';
export function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
const host = req.headers.get('host') || '';
const pathname = req.nextUrl.pathname;

// 모바일 기기 감지
const userAgent = req.headers.get('user-agent') || '';
Expand All @@ -13,8 +12,19 @@ export function middleware(req: NextRequest) {
);

if (isMobile) {
// 모바일 서브도메인으로 리다이렉션
url.hostname = `m.${url.hostname}`;
let newHostname: string;

if (host.startsWith('www.')) {
// 호스트네임이 'www.'로 시작하면 'www.m.'으로 변경
newHostname = `www.m.${host.slice(4)}`;
} else {
// 그렇지 않으면 기존 방식대로 'm.'을 앞에 추가
newHostname = `m.${host}`;
}

// 호스트네임을 변경하여 URL 업데이트
url.hostname = newHostname;

return NextResponse.redirect(url);
}

Expand Down

0 comments on commit c682e49

Please sign in to comment.