404 opens on default language

I have a multi-language site and when the page is missing on the second language, I get 404 pages in the main language. I expect to get 404 on the second language.

I have
defaultContentLanguageInSubdir: false

I use separate 404.md with layout: '404' in frontmatter, 404.md presents in content/ for each language and the layout in layouts/_default/404.html

Well, I’ve tried to add

redirects:
- from: /en/**
  status: 404
  to: /en/404.html
- from: /**
  status: 404
  to: /404.html

and moved /public/404/index.html to /static/404.html
and /public/en/404/index.html to /static/en/404.html

In the end, I got the same result. When I browse English pages and click on a missing page, I get 404 for the main language (not an English page).

Have I missed something?

Please note the 404.html file should be at the root of the layouts folder. If you need it to work on multilingual sites, you need to translate it using i18n files.

Alternatively, you can create files like 404.md, 404.es.md etc and add the url: 404.html in the front matter. Then add language specific content in there. It will be copied to the public folder of each language.

This helps to change the 404.html for both languages, but this does not resolve the problem.
For some reason, this returns a 404 for the default language.

In the language switcher, the current language becomes defaultLanguage, too. I can switch the language to en (the second language here), and I get the English 404, and the switcher becomes correct. But such behavior looks wrong.

And the /public/en/404.html contains a proper 404 page.
As well as /public/404.html for default language

Share your code for others to help. See Requesting Help.

This works great:

git clone --single-branch -b hugo-forum-topic-53209 https://github.com/jmooring/hugo-testing hugo-forum-topic-53209
cd hugo-forum-topic-53209
hugo server

layouts directory structure

layouts/
β”œβ”€β”€ _default/
β”‚   β”œβ”€β”€ baseof.html
β”‚   β”œβ”€β”€ home.html
β”‚   β”œβ”€β”€ list.html
β”‚   └── single.html
β”œβ”€β”€ partials/
β”‚   β”œβ”€β”€ language-switcher.html
β”‚   └── menu.html
β”œβ”€β”€ shortcodes/
β”œβ”€β”€ 404.en.html
└── 404.html  <-- 404 template for default content language

site configuration directory

config/
β”œβ”€β”€ _default/
β”‚   β”œβ”€β”€ hugo.toml
β”‚   β”œβ”€β”€ languages.toml
β”‚   β”œβ”€β”€ menus.de.toml
β”‚   └── menus.en.toml
└── development/
    └── server.toml

config/development/server.toml

# See https://gohugo.io/getting-started/configuration/#_404-server-error-page

[[redirects]]
from = '/en/**'
status = 404
to = '/en/404.html'

[[redirects]]
from = '/**'
status = 404
to = '/404.html'
2 Likes

thanks for a real example,
so right, here appears the same issue

I’m surfing the English version, and when I open a non-existing page, I get a DE 404 page, instead of EN.

When you ask for a page in the German language that doesn’t exist, you get a German 404 page.

When you ask for a page in the English language that doesn’t exist, you get an English 404 page.

I’m not sure what you were expecting, but the behavior above is correct.

2 Likes

Thanks, that’s exactly what I have expected. The problem was with gohugo version. In one of my instances, it was 0.124 and it was broken. On another with 0.139 it works like expected.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.