Multiple 404 Pages For Hugo

So I am creating a site in Hugo which is technically a multisite. Two in one.
Both sites have a similar layout but different menus and content.

The page structure is something like this

content/
├── pages/
│   ├── about/
│   │   └── _index.md
│   ├── about-rjis/
│   │   └── _index.md
layouts/
├── pages/
│   ├── about/
│   │   └── list.html
│   ├── about-rjis/
│   │   └── list.html

Now the issue I’m facing is I can’t figure out how to create separate 404 pages for them. What I did so far…

Changed the dir structure to this

content/
├── main/
│   ├── about/
│   │   └── _index.md
------- Other Dirs ------------
├── rjis/
│   ├── about/
│   │   └── _index.md
-------- Other Dirs ------------

Same in the layouts dir then I created two 404.html and .md files one of them in the root of main/404.html and other in rjis/404.html. The .md files has this frontmatter in them

---
title: "404 Not Found"
layout: "404"
---

I also created a netlify.toml file and put this code in it.

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

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

but It’s not working, I see a blank page

This works but this shows only one page as a global 404 page not the other, that I have in rjis dir.

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

Idk what I am doing wrong. I also created the same topic on Hugo but not really going anywhere

@YasirRWebDesigner Are you able to post a link to the site on Netlify?

https://gleaming-fenglisu.netlify.app, Also one more thing home page, stops working from time to time. like it works fine no local host, but not on netlify. The other pages work fine.

@YasirRWebDesigner I asked because I wanted to check if the files actually exist in the locations you’ve specified.

They don’t seem to?
https://gleaming-fenglisu.netlify.app/main/404.html
https://gleaming-fenglisu.netlify.app/rjis/404.html

Can you run your build locally and make sure it is actually outputting a /main/404.html and /rjis/404.html into your Publish directory.

but they do



building locally does output the files

@YasirRWebDesigner Those are different URLs, notice the trailing slash.

The rjis one you reference is actually here:
https://gleaming-fenglisu.netlify.app/rjis/404/index.html

I’m not sure what’s going on with the main one, which isn’t here either:
https://gleaming-fenglisu.netlify.app/main/404/index.html

You really should just check what files are being output by your build, as it’ll shine a bit of a light on it.

As you’ve shown in the screenshots you provided while I was writing my reply, your files are actually called index.html and are in folders called 404, that isn’t what the Netlify redirects documentation states, nor what your redirect rules point to.

So you’ll need to make things align, by either changing where they output, or where the redirect points.

I made some changes these urls work now,

https://gleaming-fenglisu.netlify.app/rjis/404/
https://gleaming-fenglisu.netlify.app/main/404/

But I am kinda still where I started from, if there’s an actual 404 it only shows the page from main/404 and not the other.

What I am trying to do if is there’s an 404 on main or / it shows the 404 from main/404 but if there’s an error on url/rjis/ it show the 404 from rjis/404 kinda like this

@YasirRWebDesigner I’m not too sure what you’ve done, as you just mention “changes”, but what I see is:

https://gleaming-fenglisu.netlify.app/rjis/404/index.html = Status 200

https://gleaming-fenglisu.netlify.app/rjis/404.html = Status 404

https://gleaming-fenglisu.netlify.app/main/404/index.html = Status 200

https://gleaming-fenglisu.netlify.app/main/404.html = Status 404

So presumably you changed the to on the redirects:
from /main/404.html to /main/404/index.html ?
from /rjis/404.html to /rjis/404/index.html ?

What do your redirects look like now?

actually, no here I making the repo public temporarily.

@YasirRWebDesigner Checking that change, it should resolve to the same thing I mentioned, I just manually resolved to where the files actually are.

E.g. There’s no file at /main/404/, the file is at /main/404/index.html

Could you try with them pointed at the actual files?
(I can’t say if it’d make any difference, but it seems worth a shot)

I also spot that the Netlify documentation itself only ever mentions the files being explicitly called 404.html, which is how I’ve done it in the past, so it may actually be a requirement.

I think I tried something like that but Netlify couldn’t find it at all.

@YasirRWebDesigner Sorry I didn’t mention, I don’t work with Hugo, so I’m unfamiliar with precisely how it’s outputting files.

But my guess, comparing what you’ve shown:

pfft
url: "/main/404"
url: "/rjis/404"

test 404
url: 'main/404'
url: "rjis/404"

Is that because neither specifies producing an index.html file, that it’s “just what is happening” and both have output the files to:

/main/404/index.html
/rjis/404/index.html

Neither of which is 404.html

in test 404, it was https://github.com/YasirRWebDesigner/laserExperts/blob/ae66c7ce28905b386df40a9ac9ea594c35411c9b/themes/laserExp/layouts/rjis/404.html

@YasirRWebDesigner Excuse me if I’m wrong, but that appears to be a source file though, not an output file from a build.

It’s at a path of themes/laserExp/layouts/rjis/404.html

Netlify will only automatically find one 404 file placed explicitly at /404.html
All the others have to be achieved via the redirects.

oh I think I kinda get your point, you are saying the netlify only looks for a page called 404.html and my files are 404/index.html but according to the docs shouldn’t work for urls as well?

# Show a custom 404 for this path
[[redirects]]
from = "/ecommerce"
to = "/store-closed"
status = 404

so technically

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

this should work right?

@YasirRWebDesigner I’d expect it to work yes, but as I said earlier, it seems worth a shot try some basic things, even though it may make no difference at all.

Especially when you factor in that a URL of /store-closed wouldn’t resolve to /store-closed/index.html, but to /store-closed.html

If the redirect is pointed at another URL, there’s clearly other handling that needs to fire.

okay, so here’s what did and now it’s according to what you were saying to have a 404.html file in main and rjis. slightly different the main/404.html is in layouts/404.html like normal 404 file.

First in the .md files I removed the url: frontmatter and added

aliases:
  - /rjis/404.html

what this does is on top of a file in 404/index.html it also creates an other file named 404.html in the rjis dir.

I removed the url front matter because I think it was going to /dir/404/ and not /dir/404.html and guess what :smiley:

It still didn’t work not only that going to url/dir/404.html doesn’t show any pages, even though there’s a file

on the main/ or / the 404 page shows fine even if you goto url/404.html but that worked even before so :person_shrugging:

netlify.toml file

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

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

@YasirRWebDesigner It seems to work for me now?

This page ends up on the main 404 (the one that says oops):
https://gleaming-fenglisu.netlify.app/main/this-page-does-not-exist

This page ends up on the rjis 404 (the one that says rjis):
https://gleaming-fenglisu.netlify.app/rjis/this-page-does-not-exist

It’s working finally after 20 commits and 24 hours, it’s working… T^T

1. Create two 404.html files 1st in layouts/ 2nd in your other dir for me rjis/.

2. Create .md file content/rjis/404/_index.md

---
title: "404 Page Not Found"
layout: "404"
---

3. Add to netlify.toml file

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

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

Edit: It is important that you name them 404.html otherwise it won’t work, the .md in 404 dir don’t use fontmatter like url it won’t work, if there are multiple files listed as 404 in the same 404 or if they aren’t in the right place. At least this what I think is going on here