In this lesson, we utilize a URL shortening service to create an application. How does a URL shortener work?
A URL shortener is a service that generates a unique short URL, usually around ~10 characters long, which is mapped to a longer URL and leads to the same destination as the longer URL. The information is typically stored in a table or hash map in key-value pairs, where keys are the short URL, and the values are the original, longer URL.
For example, say you used the bit.ly URL shortening service to shorten some long URL that is 100’s of characters long. The service will generate a unique code comprised of alphanumerical characters (A-Z, a-z, 0-9) for the key. The short URL is typically the domain name,
'bit.ly', plus the generated code, like
'abc123', making the full URL
'bit.ly/abc123'. Then, the original, longer URL will be mapped to this shortened URL in the database. It will look something like the following,
TABLE: short URL long URL bit.ly/abc123 example.com/verylongURL... ...
Now, when anyone goes to the short URL, it will direct them to the same destination as the long URL it is mapped to.
The uniqueness of short URL’s is usually guaranteed because of the sheer number of possible combinations of alphanumerical characters as codes. And, on the small possibility of a collision, the service would usually just regenerate a new code until it finds one that is not yet taken.