All posts
Platform migrations

Redirects in Next.js: next.config, Middleware and Edge Options

Next.js redirects compared: next.config rules, middleware lookups and host-level redirects on Vercel or Cloudflare, and which suits a large migration map.

By Mehdi Hassan6 min read

The short answer

Nextjs redirects are usually set in next.config.js with a redirects function that returns source, destination and permanent fields. Permanent rules return a 308, which Google treats like a 301. For migration maps with thousands of rows, move the lookup into middleware or host-level redirects so the config stays small and fast.

Key takeaways

  • next.config.js redirects are the default: versioned, reviewed and applied before any page renders.
  • Permanent: true returns a 308. Set statusCode to 301 if a tool or partner insists on it.
  • Pattern rules with path parameters can replace hundreds of one-to-one rows.
  • Middleware with a lookup table scales to large maps without bloating the config.
  • Host-level redirects work even if the app is down, which suits old domains.

Part of Website Migration SEO Checklist: Keep Your Rankings When You Switch Platforms

What are your options for nextjs redirects?

Next.js gives you three places to put redirects: the redirects function in next.config.js, middleware that runs before a request reaches a page, and your host's own redirect layer, such as Vercel project settings or Cloudflare. A fourth option, redirect calls inside a page, is for app logic rather than migrations.

For a site migration, the choice depends on how many rules you have and who maintains them. Redirects are one step in a wider plan, so pair this guide with our website migration SEO checklist. Here we compare the options and show how to set up each one.

Option

Best for

Scales to big maps?

Who edits it

next.config.js redirects

Most sites, patterns and a few hundred rules

Gets unwieldy

Developers, via code review

Middleware lookup

Large migration maps, data-driven rules

Yes

Developers, or editors via a data file

Vercel or host config

Domain-level and simple path rules

Depends on plan

Developers or ops

Cloudflare redirect rules

Old domains, bulk lists, sites in front of any host

Yes, with bulk lists

Ops or developers

redirect() in a page

Logic like auth or moved records

Not intended for it

Developers

How do you set up a Next.js 301 redirect in next.config?

You set up a Next.js 301 redirect by adding an async redirects function to next.config.js that returns an array of rules. Each rule has a source path, a destination, and a permanent flag. Next.js applies these before it checks pages or static files.

  1. Open next.config.js

    Add an async function called redirects to the exported config object. It returns an array.

  2. Add a rule

    Each rule is an object with source, such as /old-pricing, destination, such as /pricing, and permanent set to true.

  3. Choose the status code

    Permanent: true returns a 308 and false returns a 307. If you need a literal 301, set statusCode to 301 instead of permanent.

  4. Use patterns for folders

    A source like /post/:slug with destination /blog/:slug moves a whole folder in one rule. A trailing star on the parameter matches nested paths.

  5. Deploy and test

    Redirects in the config only apply after a build. Request each old URL and check for one hop and a 200 at the end.

The Next.js documentation for the redirects option covers matching on headers, cookies and query strings, plus regex paths. For migrations, simple path rules and parameters handle almost everything.

When should you use a nextjs middleware redirect?

Use a nextjs middleware redirect when your migration map is too large or too dynamic for the config file. Middleware runs on matching requests before rendering, looks up the incoming path in a table, and returns a redirect if it finds a match.

The Next.js docs include a section on managing redirects at scale, which describes this pattern: keep the map in a data store or generated file, and check it in middleware. That way the map can be regenerated from your 301 redirect map template spreadsheet without editing the config by hand.

  • Keep the matcher narrow. Only run middleware on paths that could be old URLs, so normal pages aren't slowed down.
  • Return a 301 or 308 explicitly. The redirect helper in middleware accepts a status code.
  • Normalize before lookup. Lowercase the path and strip trailing slashes so small differences don't miss.
  • Log misses. A 404 on an old-looking path is a row you forgot to map.
Hypothetical example: a 3,000-row map from a WordPress move

For example, suppose a content site moves from WordPress to Next.js and its old URLs use dates, like /2022/04/slug. The new site uses /blog/slug. A pattern rule can't drop the date reliably for every post, so the team exports a map of old to new paths from the crawl.

Instead of 3,000 entries in next.config.js, they generate a JSON lookup at build time and check it in middleware that only matches paths starting with a four-digit year. Category and tag archives get a handful of pattern rules in the config. Both layers are tested against the full old URL list before launch.

When are Vercel redirects or host-level rules better?

Vercel redirects and other host-level rules are better when the redirect doesn't belong to the app at all. The classic case is an old domain after a rebrand: it should redirect even if the Next.js app is mid-deploy or moved to another host.

Vercel lets you configure redirects in project settings and in a vercel.json file, and Cloudflare offers redirect rules and bulk redirect lists for any domain proxied through it. Check each provider's current docs for limits on your plan. Our guide to domain change SEO explains why the old domain should keep redirecting permanently, and our subdomain to subfolder migration guide covers proxies and redirects across hosts.

What are the common mistakes with Next.js redirects?

The common mistakes with Next.js redirects are chains, conflicts with trailing-slash settings, and rules that never get tested against the real old URL list. Each one is easy to miss in a code review.

  • Temporary by accident. Permanent: false returns a 307, which tells Google to keep the old URL indexed.
  • Overlapping patterns. Rules are checked in order, so put specific rules above broad ones.
  • Redirecting to the homepage. Map each old URL to its closest match instead.
  • Forgetting query strings. Campaign URLs with parameters should still land on the right page.
  • Skipping verification. Crawl the old URL list after every deploy that touches redirects.

Which Next.js redirect option should you choose?

Choose next.config.js for patterns and up to a few hundred rules, middleware for large or generated maps, and host-level rules for anything domain-wide. Many migrations end up using two layers: patterns in the config and a domain rule at the host.

If you're moving away from Next.js instead, the rules have to be recreated on the new platform. Our guides to Framer redirects and Next.js to Framer migration cover that direction.

Frequently asked questions

Do Next.js redirects work with static export?
No. The redirects option in next.config.js needs the Next.js server or a host that supports it, so a fully static export ignores it. If you export static files, set redirects at the host instead, through its config file, project settings or a CDN rule. Test after deploy, because a silently ignored rule looks fine in code.
Can I redirect based on query parameters in Next.js?
Yes. Redirect rules in next.config.js can match on query values, headers and cookies with a has field, and middleware can read the full URL. This is useful when old URLs relied on parameters, such as an old CMS that used page IDs. Keep these rules specific so normal campaign links aren't redirected by mistake.
Should redirects live in code or in a CMS?
Migration redirects usually belong in code or at the host, where they're versioned and tested. A CMS-managed redirect list makes sense when marketers rename pages often, but it needs guardrails against loops and chains. Many teams combine both: a fixed migration map in code and a small editor-managed list read by middleware.
Do redirects slow down a Next.js site?
A single config redirect is very fast because it's resolved before rendering. Middleware adds a small cost to every request it matches, which is why the matcher should be narrow. The real speed problem is chains, where one URL passes through several hops, so test for hop count as well as status codes.

· CEO & Design Director

Copywriter, designer and developer, and founder of Designify Studio. Over five years, 60+ projects across 14+ industries, for startups that have raised $300M+.

Want this done for you? Website, $8,000.

A multi-page marketing site, designed by us, developed in Next.js and animated. $8,000, one-time, up to 6 pages. Book a free 30-minute call to see if it fits.

Schedule Call