Set Up Redirects for Github Pages
In this post, I will show you how to setup redirects on Github pages. The objective is to redirect a number of individual pages to unique destinations. For me, I moved my lessons from
https://posts.keithweaver.ca/lessons/id to be able to use GetDocumentation, but I didn’t want readers not to find the pages.
Before we dive into the details, there are two short comings of this approach. The first is that the page will return a 200 instead of a 301 (Redirect). This means it’s not instructing the system reading to follow the redirect. Second, the
jekyll-redirect-from plugin uses
http meta-refresh . Using this plugin without taking special precautions on more than 10% of your website's pages, then Google will degrade your SEO ranking.
We will be adding three files today. The first is a Jekyll configuration file. At the root of my site, I added
_config.yml. Within it, I define the plugin I want to use.
Then I want to add redirects for each of the pages I want to redirect. The first example will be
[a.md](http://a.md) at the root. This will redirect from
https://example.com. The contents of my markdown file are:
I publish those changes to my repository and you can see it in action.
It’s working as expected. It’s now time to setup redirects for sub-directories too. This will be
https://keithweaver.ca/test-sub/a redirects to
https://example.com#2. I create a sub-directory with
test-sub and within it, I add
a.md. The contents are:
Once again, the redirects works perfectly.
That’s it! The solution is not perfect, but gets the job done.