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 in the official Astro Discord, where the support channels are friendly to beginners.

Keep reading

  1. How to Deploy an Astro Site to Cloudflare Workers

    Put your Astro site online with Cloudflare Workers in four steps. This is how AstroBeginner itself is hosted, so every step has been run for real.

    Tutorials, 7 minute read, November 6, 2026

  2. How to Add Tailwind CSS to Astro

    Add Tailwind CSS to an Astro site with one command, then style your first element. Tested on Astro 7.4 and Tailwind 4.

    Tutorials, 5 minute read, November 6, 2026

  3. How to Add Images to an Astro Site

    Add an image to your Astro site, and let Astro shrink and convert it automatically. One test image went from 62 kB to 5 kB.

    Tutorials, 6 minute read, November 6, 2026

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