Saturday 25 August 2012

Do It Yourself – Making an URL Shortener



            URL Shorteners are nice little applications to have these days. Most webpages with content worth sharing have lengthy URLs. Twitter & Google have their own url shortening service. Tweets with URL’s on twitter are automatically shortened using their own service(t.co).


It’s no big deal to build one. Assuming you have a little bit of coding background, you should be getting your service running in no time. Else I recommend reading some basics on HTML, Server side processing, SQL queries. I will try to keep it as simple as possible. There’s always space for “YOUR” creative mind to step into action. I’ll leave that up to you.

Step1: Create a basic home page for your service. Something like Google URL Shortener . Let’s have a text box and a button. So the idea is, the user pastes his long url into the text box and hits the “Shorten URL” button.

Step2: At your server (any java based), capture the lengthy URL. For every request you receive you need to “GENERATE” an unique 6-8 alphanumeric characters. For the sake of simplicity let’s just start with 000001. For subsequent requests simply increment the previously generated number by 1(000002 and so on). Please be advised this is just to make this very simple. In reality you would be interested in using Base 36 encoding to generate your sequence. Head over to wikipedia's article on base 36 for some better idea at this generation strategy. This would make sure we make use of numbers 0-9 and characters a-z.

Step3: Save the mapping between the lengthy URL and shortened URL into a table. A typical DB entry will be something like
Id
Original_URL
Short_Code
1
000001

Step 4: You can now show the user the short code generated for the URL. It will be of the form
The idea is to have a very catchy & small domain name. That wraps up the generation.

Now when the user tries to access this short URL, another kind of request will need to be handled by your server. A reversal of the “generation strategy” mentioned above.

Step 1: From the DB table fetch the original URL.

Step 2: It wouldn’t make sense to display the original looooooong URL back on screen. Instead the user should be redirected to the Original_URL’s page.

“sendRedirect” method of “HttpServletResponse” interface is something to look at. Once a redirect request is served let’s just make sure we don’t overwhelm our server by having to serve the same redirection again and again. To do this make use of HTTP status codes 301(moved permanently) & 302(moved temporarily). The advantage is that these status codes are cached by browsers, so subsequent requests will be resolved locally. For further reading look into HTTP redirection codes.

If the above “ideal” approach seems to be too confusing at this point read this. Instead of server side redirection, simply send back the lengthy response to the user in the form of JSON or XML. Have a JavaScript function do this,
location.href= <original URL received from server>;

If your service seems to get very popular, you will at one point of time be stuck up with an awfully huge DB table. This will hamper your query's lookup time. You could also think about retention strategies for the URL's. Very old URL's could simple be disposed off. 

So there you have it, your very own URL shortener. 

Feel free to share your feedback. If you like my post I would love to hear from you.