# Alternative 404 page for specifc directory

**URL:** <https://talk.jekyllrb.com/t/alternative-404-page-for-specifc-directory/5624>\
**Category:** Help\
**Created:** [February 10, 2021, 1:59pm UTC](https://talk.jekyllrb.com/t/alternative-404-page-for-specifc-directory/5624 "2021-02-10T13:59:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![theleoad](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/theleoad/32/3247_2.png) [@theleoad](https://talk.jekyllrb.com/u/theleoad)\
**Post date:** [February 10, 2021, 1:59pm UTC](https://talk.jekyllrb.com/t/alternative-404-page-for-specifc-directory/5624/1 "2021-02-10T13:59:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chuckhoupt](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/chuckhoupt/32/523_2.png) [@chuckhoupt](https://talk.jekyllrb.com/u/chuckhoupt)\
**Post date:** [February 10, 2021, 3:37pm UTC](https://talk.jekyllrb.com/t/alternative-404-page-for-specifc-directory/5624/2 "2021-02-10T15:37:12Z")

</div>

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

> **[Creating a custom 404 page for your GitHub Pages site - GitHub Docs](https://docs.github.com/en/github/working-with-github-pages/creating-a-custom-404-page-for-your-github-pages-site)**
>
> //docs.github.com/en/github/working-with-github-pages/creating-a-custom-404-page-for-your-github-pages-site

---

<div class="post-metadata">

**Author:** ![theleoad](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/theleoad/32/3247_2.png) [@theleoad](https://talk.jekyllrb.com/u/theleoad)\
**Post date:** [February 10, 2021, 4:54pm UTC](https://talk.jekyllrb.com/t/alternative-404-page-for-specifc-directory/5624/3 "2021-02-10T16:54:13Z")

</div>

Thanks for your answer, [chuckhoupt](https://talk.jekyllrb.com/u/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?

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [February 10, 2021, 8:35pm UTC](https://talk.jekyllrb.com/t/alternative-404-page-for-specifc-directory/5624/4 "2021-02-10T20:35:37Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![chuckhoupt](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/chuckhoupt/32/523_2.png) [@chuckhoupt](https://talk.jekyllrb.com/u/chuckhoupt)\
**Post date:** [February 10, 2021, 9:41pm UTC](https://talk.jekyllrb.com/t/alternative-404-page-for-specifc-directory/5624/5 "2021-02-10T21:41:44Z")

</div>

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](https://alsprolog.com/foobar) (source code: [ALSProlog/404.md at master · AppliedLogicSystems/ALSProlog · GitHub](https://github.com/AppliedLogicSystems/ALSProlog/blob/master/docs/404.md)). The script just swaps the URL path into the page to create a faux Prolog exception message.
