VENNStart
Project / Sigo

Moving a car rental site to Next.js without breaking URLs

Moving Sigo from a React SPA to Next.js and keeping two generations of URLs with next-intl, plus fixes for redirect status codes and param syntax.

Lê Anh Tuấn4 min readView live site ↗
Sigo

Sigo is a car rental site we moved from a React SPA to Next.js, and the URLs were the hard part. The site had two generations of paths, the original Vietnamese ones and newer English ones, and links to both were still out there. My part of the team's work was setting up the Next.js app and its routing.

Vietnamese folders, English URLs

The SPA kept its routes in one file with two objects sharing the same keys: SGPaths with Vietnamese paths like /tai-khoan/doixe ("account / my cars"), and SGNewPaths with English paths like /account/my-cars. Old paths were registered with a RedirectUrl pointing at the new one.

The App Router maps folders to URLs, but I didn't want to rename and re-test half-ported screens. So the folders kept their Vietnamese names from SGPaths (app/[locale]/tai-khoan/doixe/page.tsx), and the remaining job was serving them under English URLs.

next-intl pathnames as a URL map

Sigo has one language, but next-intl's pathnames option still fits: it gives one internal route a different public path per locale. The internal route is the folder name, and the "localized" path is the English URL. SGPaths and SGNewPaths already shared keys, so I generated the table from them instead of typing every pair again:

// src/i18n/routing.ts
const generatePathTranslations = () => {
  const englishPaths = { ...SGNewPaths }
  const vietnamesePaths = { ...SGPaths }

  return Object.entries(vietnamesePaths).reduce(
    (translations: { [key: string]: { vi?: string } }, [key, viPath]) => {
      const publicPath = englishPaths[key as keyof typeof englishPaths]
      if (!translations[viPath] && publicPath) {
        translations[viPath] = { vi: publicPath }
      }
      return translations
    },
    {}
  )
}

export const routing = defineRouting({
  locales: ["vi"],
  defaultLocale: "vi",
  localePrefix: "never",
  localeDetection: false,
  pathnames: generatePathTranslations(),
})

The next-intl middleware rewrites /account/my-cars to the tai-khoan/doixe folder, and anyone opening the internal Vietnamese path gets redirected to the public one. What the SPA did by hand with RedirectUrl is now config.

For a while the app's only locale was named "en". The root layout renders <html lang={locale}>, so for more than three weeks every Vietnamese page went out with lang="en", until I renamed it to "vi" to match the vi_VN Open Graph locale.

Parameter syntax differs too. next-intl expects [id] like App Router folders, while react-router uses :id. I converted the entries we needed, such as /tai-khoan/edit/:id to /tai-khoan/edit/[id], and left both formats in one file. That caused a bug in the redirects later.

Moving car detail off the root

In the SPA, car detail pages were at /:vehicle/:code, and blog posts and CMS landing pages at /:slug. In the App Router that's two dynamic routes side by side, and any one- or two-segment URL that doesn't match a folder turns into a backend lookup. I moved car detail under its own prefix, car_detail/[slug]/[key].

Then pages went into route groups, (root) for the app and (seo) for content, and blog posts moved to (seo)/news/[slug]. Every old /:slug blog link now needed a redirect.

The first version was a page calling redirect(). In a Server Component that sends a 307, a temporary redirect, and these posts had moved permanently. The next version used a route handler with permanentRedirect (308) but read request.nextUrl.pathname, which already starts with a slash: the check against "404" was always true, and the target came out as /news//my-post. The working version:

// (root)/[slug]/route.ts
export async function GET(request: NextRequest) {
  const pathname = request.nextUrl.pathname // e.g. "/my-post"
  if (pathname !== "/404") {
    permanentRedirect(`/news${pathname}`)
  }
  return notFound()
}

A real (root)/404/page.tsx makes /404 match a static route so it never reaches this handler. The news page also reads an optional redirect URL from the post's SEO metadata in the CMS, so editors can move a post without a deploy.

next.config redirects and the matcher

A few public URLs changed on purpose during the port, and those went into next.config.ts as permanent redirects:

async redirects() {
  return [
    { source: "/account/my-trips/:slug", destination: "/account/my-trips", permanent: true },
    { source: "/account/personal-info", destination: "/users/profile", permanent: true },
    { source: "/thong-tin/:id", destination: "/users/show/:id", permanent: true },
    { source: "/tai-khoan/edit/[id]", destination: "/account/my-cars/edit/[id]", permanent: true },
  ]
},

The last entry has the wrong syntax. next.config redirects expect :id, and this rule was copied straight from the path map, so it only matches the literal text [id]. Real URLs never match it.

For the middleware matcher, I first excluded static folders one by one, and every new file meant another edit, so I switched to skipping any path with a dot:

export const config = {
  matcher: [
    "/((?!api|static|.*\\..*|_next|favicon.ico|robots.txt|sitemap.xml|config|img|uploads).*)",
  ],
}

A page URL with a dot also skips the middleware. Slugs here are kebab-case, so I accept it, but that's an assumption the code doesn't enforce.

What still needs fixing

I'd standardize the path map before porting anything: store segment names once and generate the formats react-router, next-intl, App Router and next.config each want. The broken [id] rule wouldn't exist.

The generator also doesn't check for duplicate public paths: tai-khoan/doixe and tai-khoan/danh-muc-xe both map to /account/my-cars, and the middleware can only rewrite to one (I'd guess the first entry, but I haven't tested it).

next-intl's redirect from an internal path to its public path is a plain NextResponse.redirect, also a 307. Next time I'd make every hop for a permanently moved URL a 308 and check it with curl -I, because in a browser a 307 and a 308 look the same. Canonicals are also written by hand per page, and at least one points at the wrong route. Generating them from the same path map would stop that drift.

  • Next.js
  • next-intl
  • App Router
  • SEO
  • Migration
  • Redirects

I'm Tuấn (Venn), a web developer in Ho Chi Minh City. I mostly build Next.js sites and web apps, and I take freelance work.

Work with me

More posts

  1. 01Guide4 min read

    Deploying Next.js and NestJS to AWS EC2 with Docker Compose

    How I deploy a pnpm monorepo to one EC2 box. Images get built in GitHub Actions and Docker Compose pulls them from GHCR. The server has no code.

  2. 02Guide4 min read

    How I store OAuth tokens for social media APIs in NestJS

    OAuth for Facebook, TikTok and YouTube in a NestJS API. State and PKCE, encrypted tokens, refreshing before each publish, and when to ask for a reconnect.

  3. 03vennee.dev3 min read

    llms.txt on my Next.js site, and now Lighthouse grades it

    vennee.dev has a hand-written llms.txt and an llms-full.txt built from the blog. Notes on the spec, .txt routes in Next.js, and Lighthouse 13's new audit.