A few weeks ago, I decided to set up my own URL shortener. There were a few reasons why I decided to do this, the main one being "because I can". On a more sensible note, commercial URL shortening services occasionally shut down. A good example of this was Tr.im. Whilst other services like bit.ly probably won't go anywhere for a while, I'm uncomfortable leaving data that would take a lot of work to re-create (finding every Tweet, Facebook post, blog comment etc. that I'd ever posted a shortened URL in and editing it) in the hands of a third party. Even though in practise most shortened URLs have a fairly short useful life, leaving dead links lying around looks unprofessional.
I've been interested in growing my domain portfolio for a while, and had purchased a short domain, sm88.co.uk, a few weeks beforehand with the intention of using it as a small landing page with contact and social networking details since it would be easier to give out verbally than my main domain name. At 10 characters, it's at the top end of being a sensible length for a URL shortener, but since I'm wary of buying domains with foreign TLDs it's probably the closest I'll come to buying a short, memorable name. Therefore it made sense to use it.
Next, I needed to find a script to shorten URLs. Although several commercial URL shorteners will host a domain and perform shortening, using one would defeat the point as if the service went down so would my shortened links. I needed something that would run on my own shared hosting account. There are various pre-written PHP scripts floating around that looked like they would do the job, but after a bit of research the most polished and complete solution proved to be a script called Yourls.
The Yourls website has a simple tutorial on how to install and configure the script. Although part of the process involves editing a config file by hand, which isn't particularly user-friendly, the documentation is good enough that it isn't a problem. I had the whole thing up and running in about 20 minutes, including uploading the files.
By default, Yourls allows the short version of a URL to be specified manually. If a URL isn't specified, it generates it's own sequentially starting with 1. Somehow, a shortened URL with only one character (like sm88.co.uk/1) doesn't look quite right. A quick visit to the Yourls Plugin List turned up "Random Keywords", which changes the default behaviour to generating random keywords of a given length instead of sequential characters. This makes the generated URLs look more like URLs from other shortening services. Like Yourls itself, adding the plug-in required modifying a file by hand. This wasn't particularly difficult (and gave me a good excuse to play with some settings) but it would be off-putting to the average end user.
The procedure listed above creates a perfectly usable URL shortening service. There are still things I want to experiment with, like using a bookmarklet to generate URLs without logging into the admin interface and finding an iPhone app to do the same, but neither of these are particularly pressing as I'm confident that Yourls is extensible enough to do both. One of it's other advantages is that, whilst shortened URLs use the root of the domain, the actual root (sm88.co.uk/) isn't used. At present, this is simply redirected to my nameplate site. However, at some point in the future I can redirect it somewhere else and use the domain for the purpose I originally bought it for without upsetting my shortened URLs.