Search code examples
javascripthtmlgithuburl-routingjekyll

How to make github pages url case insensitive?


Website works for
jerrygoyal.github.io/Flash-Clipboard

but not for (404 error):
jerrygoyal.github.io/flash-clipboard
jerrygoyal.github.io/FLASH-clipboard
jerrygoyal.github.io/flaSH-CLIPboard and so on
You get the idea! How can I make the url case-insensitive?

I've never worked on Jekyll and not sure if my project pages are using jekyll or not. I only created an index.html page and put inside the docs folder of the repository.

I'm using a custom domain (www.jerryfactory.com) to map jerrygoyal.github.io


Here's the URL to my Github Organisation site : https://github.com/JerryGoyal/jerrygoyal.github.io

And URL for my Github project site: https://github.com/JerryGoyal/Flash-Clipboard/tree/master/docs

I'm thinking of moving my project site content to my Github Organisation site if it's possible. So if the URL case insensitivity works for only Organisation site it's fine.


Ref: Org and Project Site in Github


Solution

  • There is no direct way to make github page URLs case-sensitive. But, you can use following hack:-

    Redirect from 404 page with mixed-case or upper-case URL to lower case URL. Steps to achieve this:-

    • Just go to your root repo (your_username.github.io).
    • If not already exist then create a 404.html file and add the following script in it.
    <script>
        window.onload = () => {
            currentURL = window.location.href;
            lowerCaseURL = currentURL.toLowerCase();
            if (currentURL != lowerCaseURL) {
                location.replace(lowerCaseURL);
            }
        };
    </script>
    

    Note:- Make sure your pages/repo name are always in lower-case.

    Logic explained with example:-

    If your URL is:-

    anmol53.github.io/bmi-tracker

    and someone tried following URL:-

    anmol53.github.io/BMI-Tracker

    By default he/she will get 404. Now we will redirect him/her to anmol53.github.io/bmi-tracker by changing case of current URL by using above script.