Astro fails the build with “A redirect must be given a location with the Location header” when a prerendered page or endpoint returns a 3xx Response without a Location header. A static build turns every redirect into an HTML page pointing at the destination, so it needs to know where to send people. Use Astro.redirect('/new-path/'), which sets the header for you, or add Location to the response yourself.
Key Takeaways
- It’s a build-time error from static generation of a page that returned status 300 to 399.
Astro.redirect(url, status)always setsLocation; hand-built responses often forget it.- Header names are case-insensitive, but the value must be a non-empty URL.
- Config-level
redirectsnever trigger it; check your pages and endpoints.
The exact error
A redirect must be given a location with the `Location` header.
The redirect `Response` has no `Location` header. Use `Astro.redirect()` to create redirects, or add a `Location` header to the `Response`.
Astro 7.4 added the second line. Earlier 7.x releases print only the title.
What causes it
During astro build, Astro renders each prerendered route. When the response status is between 300 and 399, it reads the Location header to write the redirect page. No header, no destination, so the build stops:
---
// src/pages/old-pricing.astro (broken)
return new Response(null, { status: 301 });
---
// src/pages/feed.ts (broken)
export function GET() {
return new Response(null, { status: 308, headers: { location: '' } });
}
The second one fails too: an empty value counts as missing.
The fix
In a page, use Astro.redirect():
---
// src/pages/old-pricing.astro
return Astro.redirect('/pricing/', 301);
---
In an endpoint, use context.redirect(), or build the response with a real Location:
// src/pages/feed.ts
import type { APIRoute } from 'astro';
export const GET: APIRoute = ({ redirect }) => redirect('/rss.xml', 308);
// or, equivalently:
// return new Response(null, { status: 308, headers: { Location: '/rss.xml' } });
Response.redirect() from the Fetch API also sets the header, but it requires an absolute URL, so the Astro helpers are simpler.
Better: move fixed redirects to config
If the page exists only to redirect, delete it and declare the redirect in astro.config.mjs:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
redirects: {
'/old-pricing': '/pricing/',
'/feed': { status: 308, destination: '/rss.xml' },
},
});
Config redirects always have a destination, appear in your host’s redirect file when the adapter supports it, and can’t hit this error. The Astro redirects tip covers the options, and custom redirect pages shows how to style the HTML page a static redirect produces. For site-wide moves, use the site migration SEO checklist. Our pages and routing docs explain where pages sit in the static Komal themes. The official entry is in the Astro error reference.
FAQ
How do I redirect in an Astro page?
Return Astro.redirect('/destination/', 301) from the frontmatter. In a static build this writes an HTML redirect page; on demand it sends a real 301 with a Location header.
Why does a 3xx Response fail only during astro build?
The static build has to turn the response into a file, which needs the destination. On-demand routes pass the response straight to the browser, so a missing header there gives a broken redirect rather than a build error.
Does Location need to be an absolute URL in Astro?
No. Astro.redirect() accepts root-relative paths such as /pricing/. Only the Fetch API’s Response.redirect() insists on an absolute URL.