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!

  • K

    Your save-process is too complicated. You don’t need to create a hash. Just increment the id by one for each URL. And then you use one of the smart “character to decimal” converters out there, that converts base87 (or whatever) to the internal numerical value.

    Much more simple, and you avoid checking for duplicates. The first users of your service also gets 2-3 letter id’s, so that makes it more attractive.

  • Good info well i run a urlshorting service i found this when i was researching the comption as my site is very new but doing ok as not done much promting on as of yet. Why i made this site well to offer a free service to web masters and i did not like the sites i use to use when i made small er urls the server cost are not cheap unless you have a decicated server as i have that way i can run my own sites with out much cost at all

  • I prefer because you can specify tags to use in your URLs instead of generating random ones.

  • jgoto

    At least for now, TinyURL is a lot more reliable than Twitter is. If twitter had its own service it had better be put on a different server because you wouldn’t want the shortened links to stop working everytime Twitter goes down.

  • mvh

    Nice post! Was thinking about that tinyurl idea myself. But instead of making money with it by framing it in with a banner on top. I figured you could also make a database of affiliate links from several affiliate programs, and try to match these with the url you transform. If it exists (of you can build an affiliate url) change the landingsurl to the affiliate url. The user won’t see the difference, (most of the time) a cookie is placed for returning visit conversion. If it doesn’t exists just use the regular url. The sheer volume would create enough profit (i think).

    As an extention you could have users build their own tiny affiliate links and make money of it as a revenue share model. Most affiliate programs I work with have a unique url reference.

    Of course if you really want to set it up big, you could start your own affiliate program…

  • Baishampayan Ghose

    MD5 is a hashing, and not an encryption algorithm. Anyway your way of creating the permalink is way too complicated. Real url shortening services use a BASE64 (or something similar) encoding of the DB id.

  • Andreas

    I don’t understand why Twitter is shortening URLs at all. They should just excempt URLs from the 140 chars limit. After all, linking is what drives the web and should not be punished.

  • @seo consultant
    actually, tinyurl also lets you use your tags. On their page, the box under your URL.

  • Great idea. If it’s so simple, any of you coders want to give me a price for putting one together?

Developer Resources