Write your own URL shortener
With Twitter and its 140 characters limit leading all the trends, URL shortening services are in extremely high demand. We have seen TinyURL in the past, now Bit.ly, Is.gd, Tr.im and dozens of others are joining the party to share the cake. Honestly saying, to me they’re all the same (except for ➡.ws for its cool name). With that being said however, it is interesting how they are doing it out there – what mechanism / algorithm / buzzword-here?
I curiously did some “research”es (well, another buzzword), and it seems in order to create the shorten URLs they are following the same steps:
- Insert the original (long) URL into the database
- Get the insert ID. If the URL already exists, take its row ID. Let’s say we got 123456.
- Convert the ID 123456 into something even shorter, let’s say “am4k”
- Make use of Apache’s mod_rewrite so that any request to http://host.com/am4k will reach http://host.com/redir_script.php?code=am4k instead.
- In redir_script.php the value “am4k” is converted back into base10′s 123456 and the corresponding URL is queried back from database
- If a URL is found, redirect the request to it.
It is simple enough… except for step 3 and 4. Which conversion is there, and how is the .htaccess written? Read more »

