Fix Case Sensitive urls on Github Pages (Jekyll) sites

31 May 2019

Today I decided to fix the problem that concerns my site, namely the addresses are case sensitive. I resolved the problem using a little script on 404 pages:

var jekyllPages = [];

// Posts
{% for post in site.posts %}
jekyllPages.push("{{ site.url }}{{ post.url }}");
{% endfor %}

// Pages
{% for page in site.pages %}
jekyllPages.push("{{ site.url }}{{ page.url }}");
{% endfor %}

function jekyllFixCaseSensitiveUrl() {
    var url = window.location.toString();
    
    if (url.slice(-1) === "/"){
        url = url.slice(0, -1);
    }
    
    var page = jekyllPages.map(function(value) {
        return value.toLowerCase();
    });
    var i = page.indexOf(url.toLowerCase());
    
    if (i !== -1) {
        window.location = jekyllPages[i];
    }
}
window.onload = jekyllFixCaseSensitiveUrl;

It forwards user to “correct version” of page when he opens 404 page.