Astro throws “Forbidden rewrite to a static route” when a page, endpoint or middleware that renders on demand calls Astro.rewrite() or context.rewrite() with a path whose page is prerendered. A prerendered page only exists as an HTML file after the build, so the server cannot render it at request time. Add export const prerender = false to the target page, or use Astro.redirect() instead of a rewrite.
Key Takeaways
- Rewrites render another route’s component in the same request, on the server.
- With
output: 'server', a rewrite target must also be rendered on demand.- The error message names the target component; that is the file to change.
Astro.redirect()works with static targets because the browser makes a new request.- i18n fallback routes are the one exception Astro allows.
The error
Forbidden rewrite to a static route.
You tried to rewrite the on-demand route '/account' with the static route '/login', when using the 'server' output.
The static route '/login' is rendered by the component
'src/pages/login.astro', which is marked as prerendered. This is a forbidden operation because during the build, the component 'src/pages/login.astro' is compiled to an
HTML file, which can't be retrieved at runtime by Astro.
Hint: Add `export const prerender = false` to the component 'src/pages/login.astro', or use `Astro.redirect()`.
Why it happens
A rewrite does not send the browser anywhere. The server renders the target route’s component and returns it under the original URL. That only works if the target’s component is in the server bundle. A page with export const prerender = true (or any page in a static-by-default project) is rendered once during the build and written to dist/ as HTML; its component is not available to render later.
A typical trigger is middleware or an on-demand page that rewrites unauthenticated users to a login page:
---
// src/pages/account.astro
export const prerender = false;
if (!Astro.locals.user) {
return Astro.rewrite('/login');
}
---
<h1>Your account</h1>
---
// src/pages/login.astro
export const prerender = true;
---
<h1>Log in</h1>
Fix 1: render the target on demand
Make the target page on-demand too:
---
// src/pages/login.astro
export const prerender = false;
---
<h1>Log in</h1>
The rewrite now works, and /account keeps its URL while showing the login form. This is the right fix when the target page reads cookies, Astro.locals or other request data anyway.
Fix 2: redirect instead
If the target should stay static, send the browser there:
---
// src/pages/account.astro
export const prerender = false;
if (!Astro.locals.user) {
return Astro.redirect('/login/?next=/account/');
}
---
The browser requests /login/ as a new page, which the host serves from the prebuilt HTML. The URL changes, which is usually what you want for a login step anyway. The same applies in middleware with context.redirect().
Choosing between them
Use a rewrite when the visitor should stay on the same URL, for example a custom “not available in your region” page at the requested path. Use a redirect when the destination is a real page of its own. Our Astro rewrites guide compares the two, the middleware guide covers rewrites from onRequest, and the hybrid rendering tip explains mixing static and on-demand pages. The pages and routing docs cover the file structure, and the official rewrites reference lists the rules. Most Astro themes are fully static, so this error only appears once you add an adapter and on-demand pages.
FAQ
What is a rewrite in Astro?
Astro.rewrite() renders a different route’s content in the same request without changing the URL in the browser. It is available in pages, endpoints and middleware.
Can I rewrite to a static page in Astro?
Not from an on-demand route in a server build. Either make the target page on-demand with export const prerender = false, or redirect to it.
Does this error happen in a fully static site?
No. In a static build every page, including the one doing the rewrite, renders at build time, so rewrites between them are allowed.