🎓 A1Learner
🔍
0 of 50 lessons completed

Next.js Redirects

A redirect sends an old address to a new one. The reader lands on the new page without typing it.

Two redirects in the config

Each row names an old address, a new address, and permanent: false. Both rows point at /books.

async redirects() {
  return [
    { source: "/old-books", destination: "/books", permanent: false },
    { source: "/welcome", destination: "/books", permanent: false },
  ];
}

Both redirects, checked

The check asks the production server for both old addresses. Each one answers 307 and points at /books.

curl -s -o /dev/null -w "old-books:%{http_code} -> %{redirect_url}\n" http://localhost:3104/old-books
curl -s -o /dev/null -w "welcome:%{http_code} -> %{redirect_url}\n" http://localhost:3104/welcome

Example

console.log("/old-books → 307 → /books");
console.log("/welcome → 307 → /books");
console.log("Both moves are short-term: permanent is false.");

Key points

  • Redirect: an answer that sends the reader from one address to another
  • redirect() is the call that sends a reader away from a page. It runs on the server.
  • next.config.ts can hold redirects too. They run before any page loads.
  • The old address /old-books sends readers to /books. The answer is status 307.
  • /welcome sends readers to /books too, also with 307.
  • 307 means a short-term move. It comes from permanent: false.

In this example, two old addresses send readers to the same new page. Both answer 307, and both point at /books. The 307 comes from permanent: false in the config, which marks each move as short-term.

Note

Both moves here use permanent: false. That choice is what makes the answer 307.

Try it Yourself

Edit the code, then press Run.

Running is simulated · TypeScript Playground

Exercise

Why do /old-books and /welcome answer 307?