Question presented to candidate: "Explain the difference between a redirect and a rewrite, and all the places in Next.js where you can do each one."
What a strong answer should cover:
- A redirect answers with a 3xx status and a Location header, so the browser requests the new URL and the address bar changes.
- A rewrite serves the content of another path (or another server) while the URL stays the same.
- In next.config:
redirects()(permanent gives 308, otherwise 307),rewrites()with beforeFiles, afterFiles and fallback phases, andheaders(). - In code:
redirect()andpermanentRedirect()in Server Components and Server Actions, andNextResponse.redirectorrewritein Proxy. - Rules can match on paths, wildcards, headers, cookies and query strings with
hasandmissing.
Clarifying questions expected:
- "Is the move permanent?" β permanent redirects are cached by browsers and pass SEO signals.
- "Does the decision depend on the request (cookie, locale)?" β then Proxy or code, not static config.
Code / implementation expected: Yes β config rules and a redirect in code.