Alternative 404 page for specifc directory

I have a directory called /projects where I store some md files to show a project. When I reach some /project/file that does not exist it show my 404 page, that’s the normal behaivior. But I want to change it, if someone types a /project/file that does not exist I would like to show an “alternative 404” page for the projects. How can I achieve this? Any ideias?

It depends on how you’re hosting the site. If you’re using Github Pages, you can add a custom 404.md page:

Thanks for your answer, chuckhoupt.
But as said, I already have a custom 404 page… And that’s alright. My question is if can I have an alternative 404 page only for my projects collection. If someone goes to /projects/WRONGPROJECT, and this doesnt exist, in other words, in my directory doesnt have a WRONGPROJECT.md, I would like to show a 404 page different from the root one. I would like to show: THIS PROJECT DOESNT EXIST…

Is there any way to achieve that?

Probably not with limitation of Jekyll and GH Pages. If you had access to Apache or Nginx config you could write a rule for a subpath that uses a specific 404

On Netlify you could see if redirects gives you that control. More than redirects in a Jekyll plugin.

Otherwise you can sprinkle in some JS that looks for the referrer path and shows different content if applicable

https://www.w3schools.com/jsref/prop_doc_referrer.asp

Like if you go to /abc/xyz.html and go to /404.html then you can detect /abc/ and use that.

Or more generally, from any bad path you can use the referrer and tell the user that the path requested doesn’t exist e.g /abc/xyz.html or /a/b/g/h.html

Yea, as @MichaelCurrin notes, on Github Pages, the only option is to use Javascript to enhance the page. The variable document.location.pathname will contain the bad URL (e.g. "/projects/WRONGPROJECT"), which can be tested for a prefix, etc.

A very simple/joke example I setup can be seen at: https://alsprolog.com/foobar (source code: ALSProlog/404.md at master · AppliedLogicSystems/ALSProlog · GitHub). The script just swaps the URL path into the page to create a faux Prolog exception message.

1 Like