How URL Shortening Works

Daily, we come across a number of short URLs while surfing web. A short URL redirects to another URL (usually a long URL). For example, the URL can be shortened to using The short URL will just redirect to the long one.

There are many URL Shortening services available, e.g.,, tinyurl and the latest one by Google. Have you ever wondered how the URL shortening works? This article will take you through the concepts behind URL shortening. I will touch upon only the high level details and will not discuss any of the implementation stuff.

Understanding Short URL

If you look at a short URL, it consists of a domain and a key:
Domain =
Key =

The key is different for each long URL. Based on the key the server redirects to the respective long URL.

How does it really works?

Let us take as the domain of the service provider for explaining purpose. Here is equivalent of,, etc.

The service provider,, maintains a map of keys and URLs in the database. Where key is a positive integer and URL is just a string. The map looks like:

  • [0, url0]
  • [1, url1]
  • [2, url2]

Where url0, url1, url2… are the long URLs. The integer is unique and always incremented when a new short URL is created. It can be made sure before creating a new short URL, if a long URL already exists in the database then don’t add it again and use the same short key.

On opening the short URL= in the browser, the request goes to the server with the key. The server checks the key in the map and redirects to respective long URL. For example:

  • -> url0
  • -> url1
  • -> url2

This is one of the techniques which can be used to implement a simple URL Shortening service.

I don’t see keys as alpha-numeric

Usually, we don’t see integers as the keys. All the services use alpha-numeric string as the key. The reason behind this is that on can accommodate more keys without increasing the length of key.


For a key length of 6: base10 can have 10^6 keys and base 62 can have 62^6 keys.

Using base62 in the above mentioned technique is not difficult to implement. Keep the same database. Store integers itself in the database. The server will get requests as key in base62 and you will have to convert base62 key to base10 before finding the corresponding long URL in the database and then redirect to the long URL.