Free auditStart free
GuideAstro · Errors

Astro 404 page

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.

4 min
read
6
sources
Oct 30, 2026
updated
Scaplet Editorial Team
author
In one line

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.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode

At a glance

4 facts from the sources below
`src/pages/404.astro` or `404.md`
Static file
`404.html`
Builds to
Set status 404 yourself
Dynamic routes
`src/pages/500.astro`, on-demand pages only
500 page
02Check it

Create the page

Astro's pages docs: "For a custom 404 error page, you can create a 404.astro or 404.md file in src/pages." It "will build to a 404.html page. Most deploy services will find and use it." The phrase to notice is "most". Astro does not promise every host serves it with the right status, so you still check yours.

Give 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.

src/pages/404.astro
---
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>
03Worth knowing

On-demand routes: set the status yourself

Astro's on-demand rendering docs show how to send a 404 from a dynamic page, by setting the status explicitly. Google's HTTP status guide says that for a 2xx response whose content suggests an error, "Search Console will show a soft 404 error", so the status matters as much as the page.

Rewrite to the 404 page deliberately

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.

Watch the middleware pattern that rewrites 404 to the homepage

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.

A rewrite to a URL that does not exist returns 404

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.

src/pages/product/[id].astro (from the Astro docs)
---
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.

04Explained

Host notes and the 500 page

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.

05How it works

Check it worked

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.

  1. 01

    Request a missing URL and read the status

    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.

  2. 02

    Test a dynamic miss

    Request a dynamic route with an ID that does not exist, for example /product/does-not-exist, and confirm the same 404 status.

  3. 03

    Follow redirects

    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.

  4. 04

    Search Console

    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.

Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Where does the 404 page go in Astro?

In src/pages as 404.astro or 404.md. Astro builds it to 404.html, which Astro says most deploy services find and use.

02Does Astro return a 404 status automatically for missing dynamic content?

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.

03Can I use one custom page for 404 and 500 errors?

They are separate files. 404.astro handles not found, while 500.astro applies only to pages rendered on demand.

Sources · 6

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • astro.build
  • google.com
  1. 01Pages | Astro Docsdocs.astro.build · checked 2026-11-01
  2. 02On-demand rendering | Astro Docsdocs.astro.build · checked 2026-11-01
  3. 03Routing | Astro Docsdocs.astro.build · checked 2026-11-01
  4. 04Deploy your Astro Site to Cloudflare | Astro Docsdocs.astro.build · checked 2026-11-01
  5. 05Deploy your Astro Site to Netlify | Astro Docsdocs.astro.build · checked 2026-11-01
  6. 06How HTTP status codes affect Google's crawlers | Google Search Centraldevelopers.google.com · checked 2026-11-01
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane