gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.18k stars 7.48k forks source link

With defaultContentLanguageInSubdir: true, a 404.html file should be created at the root #5161

Open divinerites opened 6 years ago

divinerites commented 6 years ago

In the direct relation with #5160 I open this (related ?) issue.

When option defaultContentLanguageInSubdiris set to true, a sitemap.xml file is automagically generated by Hugo at the root of the website, as well as in all language subdirectories.

I propose that the same is done for the 404.html file, which is currently (as of Hugo 0.48) only generated in language subdirectories but not at the root of the website.

See :

https://discourse.gohugo.io/t/404-html-not-generated-on-root-if-defaultcontentlanguageinsubdir-true/14048

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

martignoni commented 5 years ago

Still present in Hugo v0.53.

vitaliytv commented 5 years ago

temporary workaround: NODE_ENV=production hugo -v && cp public/ru/404.html public/404.htm

yktoo commented 5 years ago

I believe this issue is absolutely relevant. You need to use some nasty hacks otherwise.

guillaumemaiano commented 4 years ago

Would be nice to know if the "hack" is currently the recommended method to use.

Lynxiayel commented 4 years ago

I think this is not a simple problem that can be addressed with a cp hack. refer to #3075. Some multilingual sites may want to have 404 pages in different languages. This involves in-site routing, and is not solvable with a single-languaged global 404.

For example, if I have a site with all English content under /en/ and their Chinese counterparts under /zh/. If a user accessed sth. not existed under /zh/, we want to present a Chinese 404 page, and in that page, all links (eg. home/section navs) should all be pointed still to pages under /zh/. The same goes for English pages. Apparently, in this case, showing either an English or Chinese global 404 page may mess up the in-site routing.

I personally think this is something need to be addressed by the server, and hugo (as a static site generator) can hardly do anything about it.

onedrawingperday commented 2 years ago

I became aware of this issue today.

Actually there is a much simpler way than what was posted above.

Generate the project locally and then simply copy the 404.html from the language directory under /static/.

The staticDir by default lives outside Hugo’s Multilingual Mode, therefore a 404.html under the root of /static/ will be served under the root of the domain without having to use any shell command during deployment.

Guite commented 2 years ago

Generate the project locally and then simply copy the 404.html from the language directory under /static/.

I guess this will lead to problems as soon as asset URLs will change? You would need to update your static 404.html copy each time the site is rebuilt to ensure it still references valid assets.

onedrawingperday commented 2 years ago

Fair enough @Guite

I tested further and found that one can simply set the url parameter with a forward slash at the beginning, so that Hugo outputs the page under the host root, even if the project uses defaultContentLanguageInSubdir = true

e.g. url = "/404.html"

I believe that this is a non-issue that it can be closed, since it is dead easy to output the 404 page at the root.

cc: @divinerites

Guite commented 2 years ago

@onedrawingperday where do you define this url parameter? Does this require creating the 404 page within content/ explicitly? please elaborate.

onedrawingperday commented 2 years ago

@Guite

Yes. Simply create a 404.md and include the above in its front matter.

Guite commented 2 years ago

Nice solution, thank you.

tomy0000000 commented 1 year ago

@bep Can you look into this strange behavior? This has been stale for quite some time but could be fixed in a short time. A similar case worth mentioning is #5160, but for robots.txt. I'm unfamiliar with golang, so I'm not sure how to work on this problem, but if you can give some direction and hints, I can help work on this bug.

anasfanani commented 7 months ago

For 404 in multilingual site. I have this idea

  1. Create a route with cloudflare
  2. I must save user lang setting in localStorage, then create a custom 404 in static dir for redirect to /en/404.html or id/404.html
freestingo commented 3 months ago

just successfully managed to get a custom multilanguage 404 page to show up in my Vercel deployment with this setup:

CyrusYip commented 3 months ago

I summarized three workarounds to this issue in a blog post: Create Top-Level 404 Page for a Multilingual Hugo Site. I hope it will be helpful.