The Elusive Custom GitHub 404 Page
This site utilizes Google Analytics, Google AdSense, as well as participates in affiliate partnerships with various companies including Amazon. Please view the privacy policy for more details.
So you want a custom 404 page for your project page, or just any subdirectory? Well, according the GitHub help page, “you must use a custom domain.”
It’s a much desired thing. Google “github custom 404” and you’ll very likely come accross this discussion. Everyone says you can’t.
But I beg to differ. In fact, I did it. All you need is a simple redirect JavaScript on your normal 404 page.
Here’s how I did it:
- Create your normal 404 per the GitHub guide.
- Create your custom 404 page wherever you want it to be. I have one in my
tags/
subfolder. - Add the following script to your normal 404 page:
Obviously, chang the URLs to match where you want it to go. And you could use if...else if...else if
etc. to have multiple 404s.
Enjoy!
Update!
To make multiple (more than two) 404’s easier, I updated the code to look like the following:
Just put all the subDirs in the JavaScript array subDirs. Careful! Make sure you make the custom 404 page, otherwords you might end up with endless forwards. Also, if you have too many custom 404’s, this script might slow down. Perhaps sort subDirs manually and use a binary search instead of the simple for loop?