Automate Dynamic Short URLS in Jekyll

Here is my usecase. When citing websites in papers and displaying them in slides and for QR codes long URLs are a pain for people to type, break over several lines in print, an make for ugly/large QR codes. I’m looking for a way to automate generating short URLS for a site with lots of posts. Each page has a four digit unique ID

Just something as ugly as:

redirect_from: {{ post.ID | prepend: site.baseurl | prepend: site.url }}
redirect_to: {{ post.url | prepend: site.baseurl | prepend: site.url }}

I wouldn’t even mind adding front matter to each page that was something like Short_Link: and then a code.

This would allow for people to be able to have much short paper citations like:
Jane Doe, "Article," from "Fancy website," https://example.tld/1234, retrieved June 1, 2525

Does that make sense? and do you have any suggestions?

have you looked at GitHub - jekyll/jekyll-redirect-from: 🔀 Seamlessly specify multiple redirections URLs for your pages and posts. ?

it may do most of what you are looking for but might be a bit of pain to maintain all the links.

I could almost see how you could build something with javascript that could do this if the shortened urls were like You could make a json file that loops thru all the posts and builds a list of codes and urls and then use js to intercept the hash and check the json for a match and then redirect to it. But I have not ever done something like that, just sort of thinking out loud.

1 Like

I am using redirect from on the site already and it is great. However I am not using it on all pages and I dont want to add thousands of redirect_from manually but it seems like that is my best option?

1 Like

I think my idea is somewhat doable - you could try just seeing how the js could check for a hash and then if there is one check a manually created json file to see if the hash exists there and if so redirect to the matching url. Then you could go about creating the full json file that would use liquid to loop thru every post and make an array of ids and urls. I suppose getting the url could be a problem?

some basic js to check for a hash could be:

const hash = window.location.hash.substr(1)
if (window.location.hash != "") {
   //do something here to check if the hash exists in an array
// then redirect if it does
if (newURL!= null) {
            window.location.href = newURL;

if you were able to do this then it would be all automatic.

but using the jekyll-redirect seems like it would be the same as when you said:

Maybe this plugin (I’m the author) is what you are looking for: GitHub - yegor256/jekyll-shorts: Automatic generator of short links for all Jekyll pages