Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
divinerites opened this issue Sep 4, 2018 · 16 comments

Comments

@divinerites
Copy link

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
Copy link

stale bot commented Jan 3, 2019

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.

@stale stale bot added the Stale label Jan 3, 2019
@martignoni
Copy link
Contributor

Still present in Hugo v0.53.

@stale stale bot removed the Stale label Jan 3, 2019
@vitaliytv
Copy link

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

@yktoo
Copy link

yktoo commented Sep 26, 2019

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

@guillaumemaiano
Copy link

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

@Lynxiayel
Copy link
Contributor

Lynxiayel commented Mar 31, 2020

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
Copy link
Contributor

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
Copy link

Guite commented Jan 8, 2022

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
Copy link
Contributor

onedrawingperday commented Jan 8, 2022

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
Copy link

Guite commented Jan 10, 2022

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

@onedrawingperday
Copy link
Contributor

@Guite

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

@Guite
Copy link

Guite commented Jan 20, 2022

Nice solution, thank you.

@tomy0000000
Copy link
Contributor

@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.

erikinkinen added a commit to erikinkinen/erikinkinen.github.io that referenced this issue Jan 6, 2024
@anasfanani
Copy link

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
Copy link

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

  • themes/<theme-name>/layouts/404.html
    • all strings inside this template are translated with the T function (example: {{ "pageNotFound" | T }})
    • in my case, I have both en and it languages set up, so when deploying this template will generate both public/en/404.html and public/it/404.html
  • vercel.json
    • contents:
    {
      "routes": [
        { "handle": "filesystem" },
        { "src": "/en/(.*)", "status": 404, "dest": "/en/404.html" },
        { "src": "/it/(.*)", "status": 404, "dest": "/it/404.html" },
        { "src": "/(.*)", "status": 404, "dest": "/en/404.html" }
      ]
    }
    • with this file I can fine-tune what translation the user sees when navigating to an unknown page (i.e. /en/unknown-page will show the English 404 page, while /it/unknown-page will show the Italian one; the English one is used as fallback for all URLs that do not start with any language code)

@CyrusYip
Copy link
Contributor

CyrusYip commented Jun 26, 2024

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.

@bep bep added this to the v0.131.0 milestone Jul 30, 2024
@bep bep modified the milestones: v0.131.0, v0.133.0 Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests