Astro.redirect()

Astro.redirect() returns a response that redirects the browser to another URL.

Since Astro 1.0 Spec ↗

Syntax

return Astro.redirect('/login', 302)

Returns

Response — A Response with a Location header and redirect status code.

Examples

---
const user = Astro.locals.user;
if (!user) {
  return Astro.redirect('/login', 302);
}
---
<h1>Welcome, {user.name}</h1>
Output
Unauthenticated visitors are sent to /login; authenticated ones see the dashboard.

Notes

Must be returned from the frontmatter before any HTML is produced. Requires an on-demand (SSR) route or output: 'server'/'hybrid'. The default status is 302; use 301 for permanent moves.

See also