Astro.rewrite()

Astro.rewrite() renders a different route's content while keeping the original URL in the browser.

Since Astro 4.13 Spec ↗

Syntax

return Astro.rewrite('/404')

Returns

Response — A Response containing the rendered output of the target route.

Examples

---
const post = await getEntry('blog', Astro.params.slug);
if (!post) {
  return Astro.rewrite('/404');
}
---
<article>{post.body}</article>
Output
Serves the 404 page content at the original requested URL without a client redirect.

Notes

Unlike redirect(), the URL does not change and no extra round trip occurs. Useful for soft 404s, A/B routing, and i18n. Available in SSR and supported by middleware via next().

See also