URL rewriting with Github Pages

I adore Github Pages. I use them for everything I can, and try to avoid server-side code like the plague, exactly so that I can use them. The convenience of pushing to a repo and having the changes immediately reflected on the website with no commit ho…


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou

redirectI adore Github Pages. I use them for everything I can, and try to avoid server-side code like the plague, exactly so that I can use them. The convenience of pushing to a repo and having the changes immediately reflected on the website with no commit hooks or any additional setup, is awesome. The free price tag is even more awesome. So, when the time came to publish my book, naturally, I wanted the companion website to be on Github Pages.

There was only one small problem: I wanted nice URLs, like http://play.csssecrets.io/pie-animated, which would redirect to demos on dabblet.com. Any sane person would have likely bitten the bullet and used some kind of server-side language. However, I’m not a particularly sane person :D

Turns out Github uses some URL rewriting of its own on Github Pages: If you provide a 404.html, any URL that doesn’t exist will be handled by that. Wait a second, is that basically how we do nice URLs on the server anyway? We can do the same in Github Pages, by just running JS inside 404.html!

So, I created a JSON file with all demo ids and their dabblet URLs, a 404.html that shows either a redirection or an error (JS decides which one) and a tiny bit of Vanilla JS that reads the current URL, fetches the JSON file, and redirects to the right dabblet. Here it is, without the helpers:

(function(){

document.body.className = 'redirecting';

var slug = location.pathname.slice(1);

xhr({
	src: 'secrets.json',
	onsuccess: function () {
		var slugs = JSON.parse(this.responseText);

		var hash = slugs[slug];

		if (hash) {
			// Redirect
			var url = hash.indexOf('http') == 0? hash : 'http://dabblet.com/gist/' + hash;
			$('section.redirecting > p').innerHTML = 'Redirecting to <a href="' + url + '">' + url + '</a>…';
			location.href = url;
		}
		else {
			document.body.className = 'error not-found';
		}
	},
	onerror: function () {
		document.body.className = 'error json';
	}
});

})();

That’s all! You can imagine using the same trick to redirect to other HTML pages in the same Github Pages site, have proper URLs for a single page site, and all sorts of things! Is it a hack? Of course. But when did that ever stop us? :D


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou


Print Share Comment Cite Upload Translate Updates
APA

Lea Verou | Sciencx (2016-11-26T00:00:00+00:00) URL rewriting with Github Pages. Retrieved from https://www.scien.cx/2016/11/26/url-rewriting-with-github-pages/

MLA
" » URL rewriting with Github Pages." Lea Verou | Sciencx - Saturday November 26, 2016, https://www.scien.cx/2016/11/26/url-rewriting-with-github-pages/
HARVARD
Lea Verou | Sciencx Saturday November 26, 2016 » URL rewriting with Github Pages., viewed ,<https://www.scien.cx/2016/11/26/url-rewriting-with-github-pages/>
VANCOUVER
Lea Verou | Sciencx - » URL rewriting with Github Pages. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2016/11/26/url-rewriting-with-github-pages/
CHICAGO
" » URL rewriting with Github Pages." Lea Verou | Sciencx - Accessed . https://www.scien.cx/2016/11/26/url-rewriting-with-github-pages/
IEEE
" » URL rewriting with Github Pages." Lea Verou | Sciencx [Online]. Available: https://www.scien.cx/2016/11/26/url-rewriting-with-github-pages/. [Accessed: ]
rf:citation
» URL rewriting with Github Pages | Lea Verou | Sciencx | https://www.scien.cx/2016/11/26/url-rewriting-with-github-pages/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.