Static site, URL has no page
- What produces the 404
- Your host serves
404.htmlbuilt fromsrc/pages/404.astro - Your job
- Create the file and confirm your host uses it
Create src/pages/404.astro, set a real 404 status on dynamic routes, handle hosts like Cloudflare Workers, and test the status code, not just the page.
Create src/pages/404.astro (or 404.md) and Astro builds a 404.html that most deploy services find and use. For on-demand routes, set Astro.response.status = 404 or return a Response with status 404, because a friendly page served with a 200 can be treated as a soft 404. Verify the status code with curl -I.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
404.html built from src/pages/404.astroAstro.response.status = 404 or return new Response(null, { status: 404 })not_found_handling setting"not_found_handling": "404-page" under assets in wrangler.jsonccurl -IGive the page a clear message and a few useful links, not a redirect. If you add a robots or noindex tag, remember Google drops 4xx URLs from the index anyway, so the page does not need it to stay out of search.
---
import Layout from '../layouts/Layout.astro';
---
<Layout title="Page not found">
<h1>Page not found</h1>
<p>The page you asked for does not exist or has moved.</p>
<p><a href="/">Go to the homepage</a></p>
</Layout> Astro's routing docs say Astro.rewrite("/404") can indicate that a product is no longer available, and a rewrite triggers a new, complete rendering phase that re-executes middleware. They do not state the resulting status for this case, so run curl -I on a missing product URL.
The docs show if (response.status === 404) { return context.rewrite("/"); }. Serving homepage content for a missing URL is a classic cause of soft 404 reports. Check the status before you adopt it.
Per the routing docs, if the URL passed to Astro.rewrite() does not exist in your project, a 404 status code is returned. A runtime error returns 500 in production.
---
export const prerender = false; // Not needed in 'server' mode
import { getProduct } from '../api';
const product = await getProduct(Astro.params.id);
// No product found
if (!product) {
Astro.response.status = 404;
Astro.response.statusText = 'Not found';
}
---
<html>
<!-- Page here... -->
</html>The same docs page shows the second option, returning a response directly: return new Response(null, { status: 404, statusText: 'Not found' }). That sends an empty 404 body, so use the first form when you want to render your own not-found content. For a removed item that has a real replacement, the docs show Astro.redirect("/products", 301) instead.
Astro's Cloudflare deployment guide has a "404 behavior" troubleshooting entry: for Workers projects "you will need to set not_found_handling if you want to serve a custom 404 page." Its example puts "not_found_handling": "404-page" inside assets next to "directory": "./dist" in wrangler.jsonc. Astro lists no equivalent entry in its Netlify guide, so test your own host with curl -I rather than assuming.
A custom 500 page is separate: src/pages/500.astro shows for pages rendered on demand and is not available for prerendered pages. It receives an error prop, but Astro warns against displaying the error's stack because it reveals how your server code is structured.
If you moved URLs rather than deleted them, set up Astro redirects. To understand the problem a wrong status creates, read about soft 404 errors.
Run curl -I https://example.com/this-page-does-not-exist. The first line must show 404. A 200 with your friendly template is a soft 404 risk.
Request a dynamic route with an ID that does not exist, for example /product/does-not-exist, and confirm the same 404 status.
Run curl -IL on the missing URL. If you land on a different URL that returns 200, your rule is redirecting instead of returning a 404.
After a recrawl, check Search Console for soft 404 errors. Google says it stops using 4xx URLs over time, and newly encountered 404 pages are not processed.
3 questions people ask next, answered from the sources on this page.
In src/pages as 404.astro or 404.md. Astro builds it to 404.html, which Astro says most deploy services find and use.
No, you set it. In an on-demand page, assign Astro.response.status = 404 or return a Response with status 404, as the Astro docs show.
They are separate files. 404.astro handles not found, while 500.astro applies only to pages rendered on demand.
Run the free audit, then approve the fixes. Every change can be undone.