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

Canonical: https://scaplet.com/seo/astro/404

Guide Astro Astro · Errors

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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fastro%2F404%20(%22Astro%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fastro%2F404%20(%22Astro%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fastro%2F404%20(%22Astro%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fseo%2Fastro%2F404%20(%22Astro%20404%20page%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

## 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

_01_Who is responsible for the status code

## Where the 404 status comes from

Situation

### Static site, URL has no page

What produces the 404

Your host serves `404.html` built from `src/pages/404.astro`

Your job

Create the file and confirm your host uses it

Situation

### On-demand route where the record is missing

What produces the 404

Nothing, until you set it

Your job

Set `Astro.response.status = 404` or return `new Response(null, { status: 404 })`

Situation

### Cloudflare Workers project

What produces the 404

Cloudflare's `not_found_handling` setting

Your job

Set `"not_found_handling": "404-page"` under `assets` in `wrangler.jsonc`

Situation

### A rewrite or redirect you wrote

What produces the 404

Not stated in the docs for every case

Your job

Test the final response with `curl -I`

_02_Check 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>
```

_03_Worth knowing

## On-demand routes: set the status yourself

Astro's on-demand [rendering](/glossary/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](/glossary/google-search-console) will show a [soft 404 error](/glossary/soft-404)", 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](/glossary/http-status-codes) 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.

_04_Explained

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

_05_How it works

## Check it worked

If you moved URLs rather than deleted them, set up [Astro redirects](/seo/astro/redirects). To understand the problem a wrong status creates, read about [soft 404 errors](/glossary/soft-404).

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](/seo-issues/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.

_01_ Where 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.

_02_ Does 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.

_03_ Can 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. 01 [Pages | Astro Docs](https://docs.astro.build/en/basics/astro-pages/) docs.astro.build · checked 2026-11-01
2. 02 [On-demand rendering | Astro Docs](https://docs.astro.build/en/guides/on-demand-rendering/) docs.astro.build · checked 2026-11-01
3. 03 [Routing | Astro Docs](https://docs.astro.build/en/guides/routing/) docs.astro.build · checked 2026-11-01
4. 04 [Deploy your Astro Site to Cloudflare | Astro Docs](https://docs.astro.build/en/guides/deploy/cloudflare/) docs.astro.build · checked 2026-11-01
5. 05 [Deploy your Astro Site to Netlify | Astro Docs](https://docs.astro.build/en/guides/deploy/netlify/) docs.astro.build · checked 2026-11-01
6. 06 [How HTTP status codes affect Google's crawlers | Google Search Central](https://developers.google.com/search/docs/crawling-indexing/http-network-errors) developers.google.com · checked 2026-11-01

## Keep learning

[All platform guides](/seo)

[Learn Astro SEO Frameworks](/seo/astro) [Related Soft 404 Crawling](/glossary/soft-404) [Read next Astro redirects Frameworks](/seo/astro/redirects) [Do Run a free site audit Free, about two minutes](/site-audit)

Nearby in platform guides
[Angular SEO](/seo/angular) [Astro SEO](/seo/astro)

More in Astro SEO
[Astro 404 page](/seo/astro/404) [Astro Google Analytics](/seo/astro/google-analytics) [Astro redirects](/seo/astro/redirects) [Astro robots.txt](/seo/astro/robots-txt) [Astro XML sitemap](/seo/astro/xml-sitemap)

Terms on this page
[Rendering](/glossary/rendering) [Google Search Console](/glossary/google-search-console) [Soft 404](/glossary/soft-404) [HTTP status codes](/glossary/http-status-codes)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
