Pages and routing
Handle redirects and missing pages
Create an explicit redirect and a useful custom 404 instead of leaving broken navigation ambiguous.
8 minute lesson
Redirect an old route when content has moved:
---
return Astro.redirect('/new-path/', 301)
---
Choose the status from the change. A permanent move usually uses 301 or 308. A temporary destination uses a temporary status.
Static redirect output depends on the adapter and host. Without platform support, a static build may produce an HTML redirect rather than an edge HTTP response. Test the deployed headers instead of assuming local navigation proves the status.
Create src/pages/404.astro for missing pages. Include a clear explanation, the site navigation, and links back to useful sections. Do not turn every unknown URL into a 200 response showing “not found”; the HTTP status matters.
Redirects should preserve useful inbound links. Avoid chains such as old → older → current. Point every old URL directly to the final destination.
After deployment, request the old URL without automatically following redirects, then request an unknown URL. Verify the status, Location header, and final page.
Lesson completed