Free Astro tutorials for complete beginners.

Beginner's Guide to Astro

How to Create a Custom 404 Page in Astro

Replace the plain 'not found' error with a helpful page of your own. It takes one file.

Tested on Astro 7.4

Cover image for How to Create a Custom 404 Page in Astro
In this guide
  1. Step 1: Create the page
  2. Step 2: Check it
  3. Step 3: Make sure your host uses it
  4. What makes a good 404 page
  5. Frequently asked questions

When someone follows a broken link, they land on an error page. Astro lets you replace the plain one with a page of your own, and it only takes one file.

Step 1: Create the page

Create src/pages/404.astro. The name matters, so copy it exactly:

---
import Layout from '../layouts/Layout.astro';
---
<Layout title="Page not found">
  <h1>Page not found</h1>
  <p>That page doesn't exist. <a href="/">Go back to the home page</a>.</p>
</Layout>

Step 2: Check it

With the dev server running, visit an address that doesn’t exist, such as http://localhost:4321/nothing-here. Your new page appears.

When you run npm run build, Astro writes it to dist/404.html. That file name is what hosts look for.

Step 3: Make sure your host uses it

Most static hosts serve 404.html without any setup. On Cloudflare Workers, you need one line in wrangler.jsonc:

"assets": {
  "directory": "./dist",
  "not_found_handling": "404-page"
}

See how to deploy to Cloudflare Workers for the full file.

What makes a good 404 page

  • Say plainly that the page doesn’t exist. Don’t blame the visitor.
  • Offer a way forward: a search box, your most popular pages, or a link home.
  • Keep your normal header and footer, so the site still feels familiar.

Our own 404 page has a search button and the six topics people ask about most. Visit any made-up address on this site to see it.

Frequently asked questions

Why doesn't my 404 page show on my live site?

Your host has to know to use it. Most static hosts, including Netlify and GitHub Pages, serve 404.html automatically. On Cloudflare Workers, add not_found_handling set to 404-page in wrangler.jsonc.

Stuck on a step?

Check the common errors guide, look up a word in the glossary, or ask the friendly people in the official Astro and EmDash Discords. How to ask so people can help.

Keep reading

  1. How to Register a Domain Name and Connect It to Your Astro Site

    Buy a domain for about $11 a year, then point it at Cloudflare or Netlify. We did this for astronursery.com, and these are the exact steps.

    Tutorials, 8 minute read, November 8, 2026

  2. How to Add Google Analytics to an Astro Site (and a Simpler Option)

    Paste Google's tag into your Astro layout with one small change, is:inline, and it works on every page. Or use Cloudflare Web Analytics, which needs no cookie banner.

    Tutorials, 6 minute read, November 8, 2026

  3. How to Add a Contact Form to an Astro Site (No Server Needed)

    A static site can't receive form submissions on its own. A free form service can. Here's a working contact form in ten lines, with spam protection built in.

    Tutorials, 7 minute read, November 8, 2026

Try "blog", "deploy" or "EmDash".