Lessons
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://keithweaver.ca/lesson/id
to 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.
1 2
gems: - jekyll-redirect-from
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://keithweaver.ca/a
to https://example.com
. The contents of my markdown file are:
1 2 3 4
--- permalink: /a redirect_to: 'http://example.com' ---
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:
1 2 3 4
--- permalink: /test-sub/a redirect_to: 'http://example.com#2' ---
Once again, the redirects works perfectly.
Conclusion
That’s it! The solution is not perfect, but gets the job done.
References
- https://stackoverflow.com/questions/39657226/jekyll-redirect-link
- https://superdevresources.com/redirects-jekyll-github-pages/
- https://github.com/jekyll/jekyll-redirect-from#redirect-to
- https://stackoverflow.com/questions/10178304/what-is-the-best-approach-for-redirection-of-old-pages-in-jekyll-and-github-page
- https://stackoverflow.com/questions/5302663/permanent-redirect-from-github-gh-pages
- https://www.mslinn.com/jekyll/6000-redirects.html