Understanding URL Shortening: The Mechanics

image

URL Shortening is pretty simple. A user simply posts their long URL then a key is created that is mapped to the long URL.

Let's take a pseudocode approach:

  1. User posts long URL.
  2. If it is a valid URL go to step 3. If not go to step 5.
  3. Create a unique key for the long URL.
  4. Save the long URL together with the unique key.
  5. If the URL is not valid return an error.

As for the redirection:

  1. User clicks on our short URL (this creates a GET request).
  2. Get the "key" part of the URL.
  3. Lookup in your storage engine for the long URL that is mapped to the key.
  4. If the lookup is successful grab the long URL and return a redirection header to the long URL's destination. If the lookup is not successful return an error (or who knows if you want to be creative and show an interstitial page together with an error and some suggestion).

That's the basic mechanism of every URL shortening service. Obviously, you can add more features to the processes above like:

  1. Analytics.
  2. User Agent detection.
  3. User location detection (be careful here due to online privacy demands).
  4. Et cetera.

Filesize: 1 KB | Last Modified: 09 Jun, 2024 5:17 pm