Short URLs

Added on: Saturday 9th May 2009

URL shortening services such as tinyURL have been around for some time now. Originally developed to overcome the problem of links wrapping over several lines in email clients they are now more prevalent because of social media applications such as Twitter

With only 140 characters per 'tweet', if you want to include a link to your website or an article on another site you don't want the URL to take up most of the message.

This is where URL shortening services like tinyURL can be useful. It is no longer alone however. A quick Google search shows there is no shortage of other services.

The top 4 services used on twitter are,, and

Each of these tries to offer something unique over the other. For example a short url on tinyurl is 25 characters long and on it is 20 characters but yet another service offers short urls that are 14 characters long.

So if you need to squeeze an extra few characters into your message then could be for you.

Is this the only reason for choosing a URL shortening service though?

For me there are two other essentials

  1. To be able to track clicks on a URL
  2.  To be able to use an API in order to create the shortened URLs.

Many of the current services do offer both of these features and there is one that I've found - - that even has the statistics available through their API.

Usually the API is very simple - you tack the URL you want to shorten onto a URL for the API and the return value is the shortened URL.

For example for tinyURL the address is:[[url]]

where [[url]] is the URL to shorten. In fact it works both ways - if [[url]] is a tinyURL (ie shortened URL) it returns the original URL.

We've now wrapped several of these APIs into a function call and made a Widget to shorten URLs in our CMS.

Of course it is not difficult to create your own URL shortening if you have access to a database and server side scripting.

  • Create a table in the database to store the original URL and a unique ID for this URL.
  • Add some server side code to take a URL and check whether it already exists in the table - if it does, grab the unique ID - if it doesn't create a new record then grab the unique ID.
  • Replace the original link with a link to a new page (with a short name like link.php) and pass as a parameter the unique ID.
  • This page then looks up the original URL associated with this ID and redirects the browser.
  • As an added bonus the page can also record visits to the page (and hence clicks on the shortened URL).

You can even go further and add a userID field to the table so that each unique ID is now for the user/URL combination meaning that you can track clicks by individuals.

For more information, contact me using the contact page.

Of course unless you've got a very short domain name in the first place you are never going to match any of the above services but you will have more control.

comments powered by Disqus