← Lessons

quiz vs the machine

Platinum1780

Frontend

Redirects and Rewrites

Send users to a new URL versus serving different content under the same URL.

6 min read · advanced · beat Platinum to climb

Two different tools

  • A redirect changes the URL the user ends up on. The browser is told to go somewhere else, and the address bar updates.
  • A rewrite keeps the URL the user sees but serves content from a different internal path. The address bar does not change.

Redirects

Use a redirect to move a renamed page, enforce a canonical form like trailing slash or lowercase, or send a logged out user to login. Server redirects carry a status code: 301 and 308 are permanent and cacheable, 302 and 307 are temporary.

Rewrites

Use a rewrite to expose a clean public URL while sourcing content from an internal route or upstream service, like proxying an API or mapping a vanity path. The user never sees the internal address.

Pitfalls

  • Avoid redirect chains and loops, which waste round trips or hang the browser.
  • Prefer permanent codes only when you are sure, since they are cached aggressively and hard to undo.

Why it matters

Choosing correctly preserves SEO, avoids broken bookmarks, and keeps public URLs clean while the backend evolves.

Key idea

A redirect changes the visible URL, while a rewrite serves other content under the same URL, and permanent codes are cached.

Check yourself

Answer to earn rating on the learn ladder.

1. How does a rewrite differ from a redirect?

2. Which status codes indicate a permanent redirect?

3. Why be cautious with permanent redirects?