Serverless URL redirects using JavaScript on GitHub Pages
As many readers of this blog are already aware, we make great use of GitHub Pages for hosting this website and several others. In particular, after FIDIT's inf2 server was finally decomissioned, Pages was the obvious choice for replacing the remaining services it offered.
Since the number and variety of applications and services hosted on inf2 server grew and diminished organically over time, what remained afterward was a collection of complex, but unrelated link hierarchies that had to be redirected to new locations (remember that Cool URIs don't change).
In particular, redirecting just the index is very simple, e.g. for ELARS Portal accessing https://inf2.uniri.hr/elarsportal/ will redirect to https://fidit-rijeka.github.io/elarsportal/ thanks to the index.html
file in the elarsportal
directory with the following contents:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url='https://fidit-rijeka.github.io/elarsportal/'" />
</head>
</html>
This is too tedious to do for every URI, but luckily there are better approaches. We briefly looked into building a "redirect site" using mkdocs-redirects plugin for MkDocs, but ultimately decided even this approach was going to be too much hassle to maintain.
GitHub Pages itself doesn't offer a proper HTTP redirection service, but there is a neat trick using 404 page. After some tinkering coupled together with reading MDN's JavaScript basics, we got to the 404.html
file with the following contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
Let's go briefly over the contents and redirect options:
- lines 12--16 handle ELARS Portal link hierarchy,
- lines 17--21 handle reStructuredText-powered CNSPSLab website link hierarchy,
- lines 22--36 handle MoinMoin-powered wiki link hierarchy, with all historical name changes taken into account,
- lines 37--42 handle two applications used in teaching Computer Networks, Computer Networks (RiTeh), and Computer Networks 2 (obviously, JavaScript will not be executed when accesed via cURL command-line interface, but the hint about the correct URI in the output should be fairly obvious),
- lines 43--44 handle our internal instance of Moodle that got replaced by Srce's, and
- lines 11 and 47 ensure that all other requests, i.e. not matched by any of the rules above, get redirected to the network operations center.
And that's it! Sure, we can extend it further to also handle renamed pages and moved files by adding nested if
s, but this is working well enough for the most of the URIs that we care about. It is also interesting to note that Google recognizes the redirects, despite the HTTP 404 status code returned by the GitHub.com
server and the need to execute JavaScript while crawling to figure out the destination URI.