If you build a multilangual Hugo site, you may want to enable defaultContentLanguageInSubdir
to make URLs look consistent. For example:
/en/post/an-english-post
/cn/post/a-chinese-post
As of Hugo v0.120.4, defaultContentLanguageInSubdir = "true"
still has the side effect that no 404 page (404.html) is generated at the root of public
directory, which causes Cloudflare Pages to redirect non-exstent pages to root /
. 404.html
at the root is needed to fix the problem.
There are three workarounds: reuse 404.html
in a subdirectory, create 404.md
, and create custom 404.html
in static
directory.
Reuse 404.html
Hugo generates 404.html
in each subdirectory. Copy an 404.html
to public
directory after building. hugo && cp public/en/404.html public
works for Cloudflare Pages, Netlify and Vercel.
Don’t copy generated 404.html
to static
directory. When 404.html
changes, you need to delete 404.html
, build the site and copy 404.html
. It’s annoying to repeat these steps.
Create 404.md
Create 404.md
in a subdirectory, such as content/en/
. Set url
to /404.html
.
|
|
Create custom 404.html
Create 404.html
in static
directory. It’s copied to public
directory during building.
A minimal example:
|
|
A more complicated example:
|
|