Redirector
March 27th, 2005 by DeWitt Clinton

To be honest, I’m not even sure why I wrote this.

Last weekend I was playing around and noticed a link to the TinyURL.com site. You may have seen those URLs before — they provide a mapping of long URLs to short URLs and perform a redirection service. For example, the URL http://tinyurl.com/623ft redirects back to http://www.unto.net/unto/.

I thought the concept was rather interesting and I thought about how I would implement it myself. The right way probably included a Perl, PHP, or Python CGI that connected to a MySQL database. However, that particular day I felt like writing some C code, playing around with autoconf, and using Berkeley DB (we are heavy users, and generally big fans, of Berkeley DB and Sleepycat over a Amazon and A9). So I wrote a fast (in terms of execution time) binary that may not be as generally flexible as a LAMP implementation.

The way it works is simple — a URL is passed in, and if the URL hasn’t been seen before it is assigned a new short ID, which is then returned to the client. If that short ID is later passed back to the server then it is mapped back to the long URL and redirection headers are sent out.

For example, the url http://unto.net/r/2 will now redirect you to my AWS product search engine. (Granted, that’s not an awe-inspiring example because the original URL is also short, but it demonstrates the point.)

You can generate your own short URLs by tacking a long URL onto the redirector in either of these forms:

http://unto.net/r/[your.unencoded.long.url.here]

http://unto.net/r?q=[your.url-encoded.long.url.here]

The application will then return a single line of text that contains a short URL that can now be passed around.

Or, just type a long URL into the form here:

Some caveats: I just did this for fun, and make no promises about the lifetime of the database. If you want to use this heavily, you may want to consider using TinyURL. That said, I don’t know anything about them, so I am neither endorsing nor criticizing them.

Also, it would not pain me in the slightest to take the redirectorx down at any time. In fact, I will take it down the second I notice it is being abused in any way. If a single spammer or hacker uses it, I will lock it down exclusively for my own use.

However, I’m happy to share the source code, which is under the GPL. So if you want to take it and play with it, feel free. But please keep in mind that this was a one-off, one-day implementation, so I made little effort to make it enterprise (or even unto.net) quality.

Also, it’s somewhat tricky to get the dependencies together (mostly Berkeley DB 4.1 or greater). I tried to document all of this in the README and create a configure script that helps out. But your mileage may vary.

Here’s a link to the source:

http://www.unto.net/downloads/redirector-0.7.tar.gz

Or, a short URL to the source:

http://unto.net/r/5

Enjoy!

Comments are closed.