Set Up Your Own Link Shortening Service with a Raspberry Pi

“Link shortening” happens when a short URL, such as http://bit.ly/2bo3XYY, points to the same web page as a longer link, such as https://en.wikipedia.org/wiki/BBC.  Short links are often used where there are a limited number of characters available, such as an SMS text or a Twitter post.  Short links are also quicker to type and neater than the associated full length links.

Two of the main providers of short links are Bitly and Google (Goo.gl).  For example, I used Bitly to create the short link in the above paragraph.  However, if you have a Raspberry Pi (or any kind of Linux server), you don’t need to use a provider.  You can create your own short links.  This article explains how.

Procedure

To provide short links, we are going to be using web redirection, just like Bitly and Google. You wont be creating billions of links, as Bitly does, but the Pi has enough capacity to create a few links for your own use.  In fact it could scale to 1000 or more without a problem.

Your Domain Name

You will need a DNS domain name to create links under. If you don’t have one, they can be purchased from many sources, such as DynDNS, noip or DtDNS. Some providers, such as DtDNS, allow the creation of additional “hostnames”. This is handy because hostnames can be quite short, allowing us to create really short links.

Examples in the procedure below assume that you have “hostname” at DtDNS called “jrt.dtdns.net“, and that the IP address points to your Raspberry Pi or other Linux/Unix server. Why “jrt” ? It doesn’t matter. Perhaps JRT are your initials. The main thing is to keep the name as short as reasonably possible, because it will form a part of all the short links you are about to create.

Install Apache

A short link is created using an Apache redirect.  First, log into your Pi and install the Apache web server.

$ sudo apt-get install apache2

Set up a web site using your chosen “hostname” (or domain name). Here is a guide, if needed. In summary: create a config file such as /etc/apache2/sites-available/jrt.dtdns.net.conf, enter your site as the “ServerName” under the Virtualhost section of the file, create the corresponding target directory (probably with a command like mkdir /var/www/html/jrt.dtdns.net, and enable the site.

Create the First Short Link

We are going to create a short link pointing to the BBC front page, https://www.bbc.co.uk. It isn’t a very long link, but this is just an example. Type the command:

$ echo "RedirectMatch 301 /0$ http://bbc.co.uk" > /var/www/jrt.dtdns.net/.htaccess

This creates the .htaccess file and inserts one line into it. Now in a browser, surf to your new shortened URL, which will be http://your.host.name/0, for example http://jrt.dtdns.net/0. Hey Presto, the BBC front page appears!

How it Works

The link works in the same way as those from Bitly or Google, by using a “301 redirect”, which is a permanent redirect from one URL to another. The short link consists of your domain name/hostname, in this example “jrt.dtdns.net”, followed by the “/1”. The “/1” is also the third item on the line that you wrote into the .htaccess file with the above “echo” command.

A Few More Short Links

Let’s try that again. Make a link to, lets say, the Nokia customer support web page. We will use a “/1” instead of a “/0”, making the short link http://jrt.dtdns.net/1. Also note the double chevron to append, rather than create the file:

$ echo "RedirectMatch 301 /1$ https://www.nokia.com/en_int/phones/support" >> /var/www/jrt.dtdns.net/.htaccess

Surf to http://jrt.dtdns.net/1, and there it is. Okay, I fancy a recipe for apple pie. One more link. This is quite a long URL. Proceed as follows.

echo "RedirectMatch 301 /2$ http://www.homesandproperty.co.uk/home-garden/food/mary-berrys-cookery-course-doublecrust-apple-pie-recipe-a105431.html" >> /var/www/jrt.dtdns.net/.htaccess

Test the link by surfing to (for example) http://jrt.dtdns.net/2. The cookery page should appear.

Links in the .htaccess File

Check the contents of our .htaccess file, and there are three entries:

$ cat /var/www/jrt.dtdns.net/.htaccess
RedirectMatch 301 /0$ http://bbc.co.uk
RedirectMatch 301 /1$ https://www.nokia.com/en_int/phones/support
RedirectMatch 301 /2$ http://www.homesandproperty.co.uk/home-garden/food/mary-berrys-cookery-course-doublecrust-apple-pie-recipe-a105431.html

It can be seen that the key to each link is the third column in the file: /0$, /1$ and /2$. We could have made these keys anything. For example if you were to change “/1$” to “/banana$“in the second line, the short link to Nokia would change from http://jrt.dtdns.net/1 to http://jrt.dtdns.net/banana (give it a try). We use numbers mainly to keep the links short, and so that we can introduce a little automation.

A Little Automation

The above process could easily be performed by a script. Something like this will take a long URL as an argument, create a short link pointing to it, and print the link.

#!/bin/bash

# shorturl_simple.sh - a script to create short links

# Base DNS name for short links
BASEURL=jrt.dtdns.net

# .htaccess file in which redirect URLs will be stored
HTACCESS="/var/www/jrt.dtdns.net/.htaccess"

id=$(wc -l < $HTACCESS)

echo "RedirectMatch 301 /${id}$ $1" >> $HTACCESS
echo $BASEURL/$id

Edit the script to use your domain name in place of jrt.dtdns.net, then run it to create a fourth short link:

$ ./shorturl_simple.sh https://www.cooperativeenergy.coop/
jrt.dtdns.net/3

A new link jrt.dtdns.net/3 is created, pointing to the Cooperative Energy web page.

If we keep running it, the script will create links with successive numbers, ie. 3,4,5, and so on. These short links can be used in emails, texts, Twitter, Whatapp etc. They will continue to work and resolve properly, so long as the entry remains in the .htaccess file, and your Pi remains powered on and Internet connected (obviously).

That’s it, really. A better version of the above script can be downloaded here: shorturl.sh. It adds some input checking and a couple of other improvements, which are explained by the rest of this article.

How the Professionals do it

How do our links compare with the short links obtained from professional services such as Bitly ? Well, Bitly and Goo.gl use “301 permanent redirects”, as we are doing. Wget verifies it:

$ wget https://goo.gl/tx5X1U
...
HTTP request sent, awaiting response... 301 Moved Permanently
$ wget http://jrt.dtdns.net/1
...
HTTP request sent, awaiting response... 301 Moved Permanently

Both our link and the Goo.gl link return a 301.

Our links were created using numbers 0,1,2,3, and so on. The professional short links all look like “https://goo.gl/tx5X1U“. That last bit is also a number, but is is written in base 62. Using such a large base, with 62 different digits, allows the resultant link (the number part) to be as short as possible. The digits are chosen from the range 0-9, a-z and A-Z. If Goo.gl used decimal numbers instead, the “short” link would be something like https://goo.gl/51260428168. Using base 62 keeps the link short, even when billions of unique links have been provided.

shorturl.sh has been adapted to use base 62. It isn’t really necessary, but our links will be slightly shorter (well, if we create more then 9), and it is nice to copy the big providers.

A Bit of Obscurity

Recall the Bitly example from the start of this article, http://bit.ly/2bo3XYY, which points the Wikipedia entry for the BBC. What happens if we change a digit, will it then point to another site? Well, yes. Change the “3” to a “4”, and you have http://bit.ly/2bo4XYY, which leads nowhere (an amusing “not found Bitly page appears). Change it again to a “5” and the same thing happens. Try a 6, however, and it resolves to page about Toronto.

In other words, Bitly (and Goo.gl) does not use every addressable short link. Sometimes it skips a few. We might want to do the same thing, and for a specific reason.

Skip a Few links

Recall the code snippet above. It produced short links such as:

http://jrt.dtdns.net/0
http://jrt.dtdns.net/1
http://jrt.dtdns.net/2
http://jrt.dtdns.net/3
http://jrt.dtdns.net/4
http://jrt.dtdns.net/5
...

…and so on. You might send such a link in an SMS text, say, pointing to your new web site or a page of interest. If a curious recipient were to change the last digit by 1 (eg. from 4 to 3), and follow the resultant link, they would see another page you have linked to but which you did not intentionally share with them. It is unlikely to happen, but this kind of thing can be discouraged by skipping a few values each time a short link is created.

The shorturl.sh script therefore skips up to 7 numbers between links. Created short links might be, for example:

jrt.dtdns.net/5
jrt.dtdns.net/d
jrt.dtdns.net/f
jrt.dtdns.net/i

The first link is “5”, then the next 7 numbers are skipped until “d” (13 in decimal), then a single value (“e”, decimal 14) is skipped, taking us to f, then 2 are skipped, and so on.

This slight randomization discourages people from “searching” for links. But it doesn’t stop them completely. A determined person would still find other links. On Bitly and Goog.le it doesn’t matter, because who created the “other” links is not known. If you want to have unguessable links, you could change $INTERVAL to some very large number in the script, making your short links a few characters longer.

Conclusion

I hope the above article was helpful. If you want to go further, and run a full short linking service with a graphical front end, there are a selection of free, self hosted solutions listed on Github. They will allow other people to create short links on your Pi or other Linux/Unix server. If you decide to implement one, remember to follow appropriate security guidelines.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.