This tutorial explains how to create and configure a custom 404 error page for your Netlify-hosted applications, ensuring it replaces Netlify’s default 404 page.
Step 1: Create a Custom 404 Page
-
Design Your 404 Page
Create a file named
404.htmlin the root of your project’s publish directory (oftenpublic,build, ordist, depending on your framework).This file will serve as your custom 404 page and will be automatically picked up by Netlify for any unresolved paths.
Example content for
404.html:404 - Page Not Found 404 - Page Not Found
Sorry, the page you're looking for doesn't exist.
-
Customize as Needed
Add your branding, styles, or links to match your site’s design.
For SPAs (e.g., React, Vue, Next.js), you can use a framework-specific component (e.g.,
404.jsfor Next.js) or generate a static404.htmlduring the build process.
Step 2: Place the 404 Page in the Correct Directory
-
Static Sites
Ensure
404.htmlis in the root of your publish directory (e.g.,public/for a vanilla HTML site orbuild/for a React/Vue app). -
Framework-Specific Sites
Next.js: Create a
404.jsor404.tsxfile in thepages/directory. Next.js automatically generates a404.htmlduring the build process.import Link from ‘next/link’;
export default function FourOhFour() { return ( <>
404 - Page Not Found
Sorry, the page you’re looking for doesn’t exist.
Return to Home </> ); }Vue/Nuxt: Create a
404.vuefile in thepages/directory (Nuxt) or handle routing in your Vue app to render a custom 404 component.React with React Router: Ensure your build process outputs a
404.htmlfile, or configure redirects (see Step 3).
Step 3: Configure Redirects for SPAs (Optional)
For SPAs, client-side routing can cause 404 errors when users refresh or directly access routes (e.g., yoursite.com/about). To handle this, configure Netlify to redirect all unmatched routes to your index.html while ensuring the custom 404 page is served for truly invalid routes.
-
Option 1: Use
_redirectsFileCreate a
_redirectsfile in your publish directory (e.g.,public/_redirectsorbuild/_redirects).Add the following rule:
/* /index.html 200 /* /404.html 404
The first rule ensures client-side routes are handled by
index.html, and the second ensures invalid routes fall back to404.html. -
Option 2: Use
netlify.tomlFileCreate or edit a
netlify.tomlfile in your project’s root directory.Add the following configuration:
[[redirects]] from = ”/*” to = “/index.html” status = 200
[[redirects]] from = ”/*” to = “/404.html” status = 404
The
netlify.tomlfile provides more flexibility and is recommended for complex setups.
Step 4: Deploy to Netlify
-
Push Changes to Your Repository
Commit your
404.html(or framework-specific 404 file) and_redirectsornetlify.tomlfile to your Git repository.Ensure your build command (e.g.,
npm run build) generates the necessary files in the publish directory. -
Configure Netlify Build Settings
In the Netlify dashboard, go to your site’s “Deploys” settings.
Verify the build command (e.g.,
npm run build) and publish directory (e.g.,build/orpublic/).Deploy your site by pushing changes to your connected Git repository or manually uploading the publish directory.
-
Test the Deployment
Navigate to a non-existent route (e.g.,
yoursite.netlify.app/does-not-exist).Confirm that your custom 404 page is displayed instead of Netlify’s default 404 page.
Step 5: Test and Troubleshoot
-
Verify Locally
For static sites, open
404.htmlin a browser to ensure it renders correctly.For SPAs, test locally (e.g.,
npm run dev) to confirm the 404 page works as expected (e.g.,localhost:3000/does-not-exist). -
Check Netlify Deployment
If the default Netlify 404 page appears, ensure
404.htmlis in the root of your publish directory and that the file is included in the deployment.For SPAs, confirm the
/* /index.html 200rule is present and takes precedence in your_redirectsornetlify.tomlfile. -
Common Issues
- Custom 404 Not Showing: Ensure the
404.htmlfile is in the root of your publish directory and not nested in a subdirectory (e.g.,/404/404.htmlwon’t work unless explicitly configured). - SPA Routing Issues: If refreshing routes causes a 404, verify the
/* /index.html 200rule is present and takes precedence in your_redirectsornetlify.tomlfile. - Framework-Specific Issues: For Next.js or Nuxt, ensure the custom 404 page is correctly named (
404.jsor404.vue) and that the build process outputs it to the publish directory.
- Custom 404 Not Showing: Ensure the
Step 6: Optional Enhancements
-
Multi-Language Support
For sites with multiple languages (e.g.,
/en/and/es/), create language-specific 404 pages (e.g.,/en/404.html,/es/404.html) and configure redirects innetlify.toml:[[redirects]] from = “/en/*” to = “/en/404.html” status = 404 conditions = { Language = [“en”] }
[[redirects]] from = “/es/*” to = “/es/404.html” status = 404 conditions = { Language = [“es”] }
Ensure the 404 pages are placed in the correct language-specific directories in the publish directory.
-
Dynamic 404 Pages
For frameworks like Next.js, use dynamic data in your 404 page (e.g., fetching suggested links via an API).
Ensure the page is pre-rendered or statically generated during the build process to avoid runtime errors.
-
Styling and Branding
Match the 404 page’s design to your site’s theme using CSS, images, or framework-specific styling (e.g., Tailwind CSS for React apps).
Notes
- Netlify automatically serves
404.htmlfrom the root of the publish directory for any unresolved paths, so no redirect rules are required for basic static sites. - For SPAs, the
/* /index.html 200rule is critical to handle client-side routing, but ensure it doesn’t override the 404 rule. - If using a framework, always test the 404 page locally before deploying to ensure compatibility with Netlify’s static hosting environment.
- For complex setups (e.g., role-based or geo-based redirects), consult Netlify’s redirect documentation for advanced configurations.
By following these steps, you’ll have a custom 404 page that aligns with your brand and provides a better user experience than Netlify’s default 404 page.