You click a short link, and a moment later you are on a page with a much longer address. It feels instant and simple. But what actually happens in between? Let’s look at it step by step, in plain words.
The basic idea
A URL shortener does just two things:
It remembers which short code belongs to which long address.
It sends visitors from the short code to the long address.
Everything else, like click counts or custom names, is built on top of these two jobs.
Step 1: Creating the short code
When you give the service a long link, it needs a short and unique code for it. There are two common ways to make one:
Random characters: the service picks random letters and numbers, then checks that nobody has used that code before.
Counting: the service gives each new link a number (1, 2, 3 and so on) and converts that number into a short mix of letters and numbers.
The second way usually uses 62 different characters: the digits 0 to 9, the lowercase letters a to z and the uppercase letters A to Z. With 62 characters, even a code that is only 6 characters long can create more than 56 billion different combinations. That is why short codes rarely run out.
Step 2: Saving the match
The service saves the short code together with the long link in a database. You can think of it as a very big address book: one column has the short code, and the other has the full address. Because people expect links to open instantly, this lookup has to be fast.
Step 3: Sending the visitor on
When someone opens a short link, the service looks up the code in its address book and then tells the browser to go to the long address. This is called a redirect, and it happens so fast that most people never notice it.
There are two main kinds of redirect:
Permanent redirect (301): browsers may remember it, so they can skip the service on later visits.
Temporary redirect (302): the browser asks the service every time. This is handy if you want to count clicks or change the destination later.
Things to think about in a real project
Duplicate codes: two different links must never get the same short code.
Checking links: only accept proper web addresses, and watch out for harmful ones.
Abuse: short links can hide where they lead, so limiting how often people can create links, and having a way to report bad ones, is important.
Reserved words: avoid codes like “admin” or “login” that could clash with the service’s own pages.
Wrapping up
A URL shortener is a great beginner project because it touches several basics in one small app: web addresses, databases, encoding and how browsers redirect. If you are learning web development, try building one yourself, then compare it with how real services work.
Disclosure: I run a URL shortener at urlshort.tr, which is what got me thinking about this topic. Questions and corrections are welcome in the comments.
Notes:
I changed the last tag to productivity because there is no code in the post now.
Add a cover image (1000 x 420 px) if you can.
The disclosure line at the end is important on DEV, because readers prefer honesty about a link to your own site. Keep it.
Keep only this one link in the post. https://urlshort.tr/
Top comments (0)