Have you ever wondered how URL shortening services work? They take a very long URL and give a shortened, crisp, and memorable URL back to the user. At first sight, it looks like magic, but it is a simple math trick.
In a single statement, URL shortening services are built upon two things:
- A string mapping algorithm to map long strings to short strings ( Base 62)
- A simple web server that redirects a short URL to the original URL
There are a few obvious advantages of URL shortening:
- Users can remember the URL; easy to maintain
- Users can use the links where there are restrictions on text length; for example, Twitter
- Predictable shortened URL length
Take a look at the following diagram:
Under the hood, these things happen silently in a URL shortening service:
- Take the original URL.
- Apply Base62 encoding on it. It generates a shortened URL.
- Store that URL in the database. Map it to the original URl (
[shortened_url: orignial_url...