As promised, we're back with the second part of our series on the Small URL Generator. Part 1 covered a very basic example that provided conversion of an URL to a shortened bit.ly-style URL. This example works great for creating small URLs based on a pseudo-base 36 encoded key, but it lacked a few of the nifty features that the big providers offer: variable key length and custom short URLs.
Variable Key Length
In part 1, we discussed our motivation for selecting the 6 character static length key. It was short enough to provide valuable URL-shortening capabilities while at the same time it provided enough unique keys (2,176,782,335 to be exact) to ensure adequate storage and minimal probability of a key conflict. This assumption does not take into account the length of URL itself. For instance, a Google Maps URL that may contain 200+ characters would have the same small URL key length as a small URL for www.cnn.com. This does not provide much benefit to shortening www.cnn.com. Now, http://www.cnn.com, which is 19 characters long, will be given a key length of 3 instead of the previous 6. The code can be tweaked to meet your needs, but the defaults should be more than sufficient for most deployments.
URL Key Length
Custom Short URLs
The ability to create custom short URLs is one of the cooler features available from URL shortening services. Our first example provided no mechanism for allowing our users to create such custom URLs. In part 2, we added a second text field in the form to allow for a custom assignment. Now http://www.f5.com/products/big-ip/ can be shortened to http://small.url/bigip instead of http://small.url/ll9ma. We love this feature as it eliminates the need to memorize alphanumeric strings as well as makes the short URL more obvious when distributed (assuming someone in the office isn’t pulling a prank).
Small URL Generator Sample Code
4:setstatic::small_url_response_header "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head> \
iRules began as an HTTP request and response manipulation language, but has matured to the point that we can write complex applications. The addition of tables in BIG-IP version 10 has opened up an entirely new realm of possibilities by which we can store key/value pairs. We hope that the Small URL Generator serves as a testament to the power of iRules and the various ways they can be used in your environments. Stay tuned for more iRule magic and fun tech tips in the near future!