404 page in root, for a websites that redirects to a subdirectory

Hi,

I would like to configure a 404 page for the root directory of a multilingual website, where the different language files reside in sub directories (/en/ and /de/).

My netlify.toml contains the following redirect for a default redirect to the German language version:

[[redirects]]
  from = "/"
  to = "/de/"
  status = 301
  force = true

and there are also redirects for language specific custom 404’s in the netlify.toml:

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

[[redirects]]
  from = "/de/*"
  to = "/de/404.html"
  status = 404

The custom 404 I want to create should only show in case someone tries to call e.g. example.com/doesnt-exist.html. The custom 404 page currently resides in /404.html.

How would I configure this in netlify.toml?
Can I use some kind of if else statements in the from = "" directive?

I am a bit lost. Thank you for your help.

Hi @dafoobar

After the current redirects you can add

[[redirects]]
  from = "/*"
  to = "/404"
  status = 404

which will capture anything not caught by the previous rules (if you add other rules, make sure they are above this rule.)

While what @coelmay suggested is correct, if you have a 404.html in the root of your publish folder, you don’t need to add any redirect. It would automatically work.

1 Like