Skip to content

Instantly share code, notes, and snippets.

@sbliven
Forked from AmrEldib/404ForCaseSensitiveURLs.js
Last active March 16, 2024 09:32
Show Gist options
  • Save sbliven/0ca4dee4e0190a6b3dc7e3d8040cc395 to your computer and use it in GitHub Desktop.
Save sbliven/0ca4dee4e0190a6b3dc7e3d8040cc395 to your computer and use it in GitHub Desktop.
Jekyll 404 page on GitHub Pages to fix case sensitive URLs
var allposts = [];
function redirectToCorrectPage() {
console.log("Unable to find page. Trying other URL cases.");
{% for post in site.pages %}
allposts.push("{{ post.url }}");
{% endfor %}
var url = window.location.pathname;
// strip trailing /
if (url.slice(-1) === "/") {
url = url.slice(0, -1);
}
var allpostsUpperCase = allposts.map(function(value) {
// strip trailing /
if (value.slice(-1) === "/") {
value = value.slice(0, -1);
}
return value.toUpperCase();
});
console.log("Looking for "+url.toUpperCase() + " in "+allpostsUpperCase);
var i = allpostsUpperCase.indexOf(url.toUpperCase());
if (i != -1) {
console.log(allposts[i]);
window.location = allposts[i];
}
}
window.onload = redirectToCorrectPage;
@sbliven
Copy link
Author

sbliven commented Jul 19, 2016

This version works for all pages (not just posts), and it correctly handles cases where the domain name might change (e.g. for local development).

@rohitnishad613
Copy link

Nice Script, But where to load?

@sbliven
Copy link
Author

sbliven commented Nov 13, 2020

Add it to your 404.html page. Original blog: https://amreldib.com/blog/FixJekyllCaseSensitiveUrlsOnGitHubPages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment