URL Shortening


If you want, you can read everything about URL Shortening on wikipedia (i.e. http://bit.ly/bgkuGT or http://d3w.io/9KilxK or http://➡.ws/蔤囦 ) but I would like to shortly (sorry, pun intended) say a couple of things.

The “URL shortener” concept is very simple: take an URL and transform it into another (shorter) URL, then use redirection to go back to the first URL. So what’s the deal? It’s the number of characters stupid! Said in other terms: websites often produce URLs so long that it becomes unpractical transferring them to your fellows or friends with other means that the digital communication tools (i.e. copy and paste, in fact it’s very difficult to dictate on the phone an URL 150 characters long full of special characters and unintelligible words). Even when the URL is humanly sized, it cannot be embedded in a discourse or printed out on your last magazine article if it’s not very very short. Another problem arose with the advent of media like Twitter, where size matters (and size is almost all the added value of the media itself): having short URLs quickly became a must, without it was impossible to communicate at all (think about having only 140 characters available).

Probably now it’s clear that URL shortening is not only something useful but can constitute a service by itself: in fact, out in the wild there are a lot of URL shortening services to which you can outsource all your shortening needs. Beware, though, that one should select carefully which service to use, because if you use it a lot on your site(s) and it goes down, no link on your site will work at all! As a proof, read this and this.

Note that, even if you doesn’t use URL shortening, your users probably will and, if you do for them, they will be grateful and reward you with some more information. In fact, another useful side effect is that you can keep track of the clicks your shortened link receives, and do all the conceivable statistics on it (and, in fact, URL shoreners implemented stats e.g. http://bit.ly/bHIVeM+ ).

Some examples of shortening:

Ok, you get it.

What You Should Do For Your Company

Not very much, thanks to bit.ly. I was thinking about internally hosting or implementing (from scratch! OMG!!) a dedicated URL shortener to be used company-wide, starting from a specific product (as you can see now here and here for example). Thinking a second time, we choose to use the brand new service offered by bit.ly, a well-known and well-enstablished URL shortener. There is also a relatively wide offer of open source shorteners (as per Google http://d3w.io/b7YIcF ), but the problem of hosting it remained (agreed that technically this wouldn’t have been a problem for the company, but in any case it’s not a core technology neither a core service so for now probably it’s better to outsource it to someone that does it as its core business).

The other very complex thing (isn’t it, IT fellows?) registering the right domain to be used by all the company, then configuring it to be used by bit.ly: the choosen domain in our case was d3w.io. Cooool, eh?

It will require a bit (…) to find the right domain to be used for URL shortening, either because it should sound cool or because you should respect TLD’s rules (e.g. many do not allow domains shorter than 2 characters).


Currently we use the bit.ly API without encapsulation; we’ll implement an internal API if it will become a widely-used service. Please refer to the official bit.ly documentation here. The only thing to do is to use the account to which the new domain is tied to: bit.ly will work as normal but using your domain instead of its own “bit.ly” domain.

Even if the API is simple (how could it be different for a service like that??), we choosed and used one pre-built C# library, as there are many already working and tested (and always reinventing the wheel can be an option but not when who pays are not you.)

With this library, shortening a URL can be as simple as the following one-liner:

shortened = Bitly.API.Bit(_bitlyUsername, _bitlyPassword, url, "Shorten");

Maybe the following function is better:

public string ShortenURL(string url) { string shortened = null; // HACK shouldn't be done by bit.ly library? url = HttpUtility.UrlEncode(url); for (int i = 0; i < 3; i++) { try { shortened = Bitly.API.Bit(_bitlyUsername, _bitlyPassword, url, "Shorten"); break; } catch { System.Threading.Thread.Sleep(1000); } } return shortened; }