So I wrote this URL shortener…

So there I was, procrastinating over a very important job that I have still to finish and I got to wondering about the short codes used for URL shortening services – you know, these ones; http://abc.com/1a4c

So I decided to make on of my own…

As is so often the case when you start researching cool mathematical stuff for a neat function, it turns out that someone’s already done all the work. In this case, I learned about the PHP base_convert function which can neatly convert boring base 10 numbers to lovely  base 35 numbers (actually, it will convert from any base number to any other base up to base 35), which is marvellous. I had originally started drafting another function to generate short-codes of a larger collection of symbols, but the codes might not always be reliable when passed through various internet chanels – I don’t think it is safe to rely on case-sensitivity for example, which would have massively extend the code range – So I decided to use the base_convert function which uses letters and numbers to represent base35 numbers.

I figure, if I use short-codes from 1 to 5 digits in base 35, that still gives me over 52.5 million codes before I have to figure a way for extending the code-base. I’ll probably run out of database before then anyway.

Once you’ve got short codes from decimal numbers sorted out the rest is a walk in the park really;

  1. Collect Urls to be shortened
  2. Store them in a database with a numerical index
  3. Use the index number for each url to generate a shortcode
  4. Do some fancy url rewriting to redirect each shortcode to it’s destination url

Since my Url rewriter is for me and not for insane “SEO specialists”. I’ve made some concessions in the way it works;

Urls aren’t redirected with a 301 http code. In fact each url in the shortener presents a page with a javascript redirect that bounces the user to their destination after a short delay. The waiting page shows the precise page that the user is about to visit, giving them a chance to change their mind and hammer the back button. After all, these short codes could be leading you anywhere.

Not redirecting with a 301 also means that I can collect more accurate analytics from each link as I can see every visit to that ‘bounce page’ before the user is passed on to their final destination. A 301 redirect is designed to be cached by browsers and subsequent visits would then bypass our page altogether, meaning we wouldn’t count all visits to our shortened urls.

The other concession I had to make was this – my url shortener doesn’t produce very short urls – regrettably the very short domain names are really damned expensive. So, for the time being, I’ve plugged it up at a short subdomain of an existing short-ish URL from my collection:

http://u.if-host.co.uk/

It currently has no stylesheet ’cause I wrote it in an afternoon, but please do try it out. Feedback will be gratefully received in the comments on this post.
Also if anybody has a cool short URL they’re not using, I would be happy to host this service on that url for you.

Incidentally, it’s my Birthday on Friday. There’s going to be cake, one way or another (and probably both – oh man I love de cake!).

One Response to “So I wrote this URL shortener…”

  1. Russ Says:

    **sigh** nobody huh?

Leave a Reply