Make Your own TinyURL Service

I was reading an article on CenterNetworks the other day, and it got me thinking. The article was about Twitter using a service like TinyURL to shorten all links posted on Twitter. The point Allen made was that, by doing so, Twitter was completely at the mercy of TinyURL’s server, and, if the TinyURL site ever went down for any reason (or, God forbid disappeared entirely) Twitter would be up the creek without a paddle (paraphrasing there). He pointed out that Twitter really ought to develop a URL-shortening service of its own, rather than relying on TinyURL.

As I said, that got me thinking. Why hasn’t Twitter developed its own URL-shortening service? What’s so difficult about it?

I’ve always kind of wondered – how does TinyURL make any money? I’m sure, with over 47 million URLs that have been shortened, the TinyURL server must have to put up with at least a slightly above-average amount of bandwidth (which isn’t cheap, as you all probably know). Is all of the site’s income based strictly on donations?

I’m also curious – what’s so special about TinyURL. The way I see it, I think it would probably take about 15 minutes to write a script that would do the same thing it does, and another 15 minutes to design and implement a much nicer site through which the users would shorten their links.

All you need is a database and a server-side language. Grab the full URL from a form (like TinyURL does) and query the database to see if that URL’s been entered before. If it has, you pull the alias you’ve already generated and return that. If it hasn’t, you generate a random number, encrypt it using something like md5, shorten it to 6-8 characters, check to make sure that alias has not been used before, place it in the database alongside the real URL and then you’re more than halfway there.

From there, you simply use ht rewrite (or something similar) to give yourself the permalink URLs that they offer, and have all of them redirect to index.php (or index.asp or pl or cgi or whatever server-side language you want to use) with a query string showing the random, encrypted number that coincides with the URL. Then, query the database for the real URL that matches up with that number and put the database result inside of a header redirect (Response.Redirect or header(“Location: $myresult”) or similar).

Bam! You’re done. You’ve got your very own TinyURL service. If you want to make money off of it, then, instead of using a redirect script, you simply load the real URL inside of an iFrame and place a random advertisement somewhere on your home page.

Sure, it might take a bit longer to get secure widgets and everything designed so that users could automatically generate shorter URLs the way Twitter does, but that shouldn’t stop someone from trying. In Twitter’s case, if they were going to use the service completely in-house, there wouldn’t really be a need for that. If you aren’t going to let someone send queries across servers, then you don’t need secure widgets that allow GET queries to run. Everything can be done through secure POST queries if you write it correctly.

You could even make money off of it by building in small advertisements that would automatically display on the page that you’ve redirected to.

There is really no excuse for Twitter not developing and utilizing its own service for this purpose.

Tech Tags: HTMLCenter CenterNetworks shorteners

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

Developer Resources