404.html Templates
When using Hugo with GitHub Pages, you can provide
your own template for a custom 404 error page
by creating a 404.html template file in your /layouts
folder.
When Hugo generates your site, the 404.html
file will be placed in the root.
404 pages are of the type “node” and have all the node variables available to use in the templates.
In addition to the standard node variables, the 404 page has access to
all site content accessible from .Data.Pages
.
▾ layouts/
404.html
404.html
This is a basic example of a 404.html template:
{{ partial "header.html" . }}
{{ partial "subheader.html" . }}
<section id="main">
<div>
<h1 id="title">{{ .Title }}</h1>
</div>
</section>
{{ partial "footer.html" . }}
Automatic Loading
Your 404.html file can be set to load automatically when a visitor enters a mistaken URL path, dependent upon the web serving environment you are using. For example:
- GitHub Pages - it’s automatic.
- Apache - one way is to specify
ErrorDocument 404 /404.html
in an.htaccess
file in the root of your site. - Nginx - you might specify
error_page 404 = /404.html;
in yournginx.conf
file. - Amazon AWS S3 - when setting a bucket up for static web serving, you can specify the error file.
- Caddy Server - using
errors { 404 /404.html }
. Details here