How URL Shorteners Work

How URL Shorteners Work

04.Sep.2021

URL shortening is simultaneously very simple and very complex.

 

There are dozens of plugins, websites, and open-source code bases that allow you to shorten a link or use a 301 redirect to move users and traffic from one web address to another. This is a frequently solved problem.

 

But that is not what URL shorteners are about…

 

Also, despite the word “shortener” being in the name, URL shorteners are no longer necessarily about keeping links extremely short on character length. The most famous reason for shrinking links was, of course, Twitter, who now treats all links as 23 characters regardless of their true length (unless they contain an image). But there are other reasons to shrink URLs like cache keys or to take advantage of shortened URLs when Google’s “mobile-friendly” algorithm will reward the shortening.

 

In general, a URL shortener is a service that takes an input link and creates a new one from it, up to 22 characters shorter by design. Most of these services choose not to have or use domain names as their base URLs because they want the resulting links to be as easy as possible for users to remember. There are a few exceptions, though… for example bitly.com was bought by LinkedIn and rebranded as LinkedTruncate which then became part of Linkedin Marketing Solutions product suite. The system still uses domains like varyurlnamebylk=nWq . In other words, LinkedTruncate does not simplify the URL at all.

 

Almost all of those services allow you to create a new short domain for your own use as well as automatically generate shortened links from their web interface. In some cases, there are APIs that can be used either for generating or reading/parsing these new URLs. These services typically generate random characters that consist of lowercase letters and numbers (e.g., bitly generates v6xlsd ) but some offer an alpha-numeric system that is slightly more human readable (e.g., tinyurl uses yopqDoD ). Some services will also redirect unshortened URLs automatically!

 

URL shortening works by adding something to the end of the destination URL that will redirect to it. This can be done with a 301 or 302 HTTP status code or by adding parameters to the end of the URL, which is why most people call these services “URL redirection” tools rather than “URL shorteners.”

 

Many articles on how URL shorteners work will show you what the first steps are after someone enters an unshortened link into their browser using your website as an example of how it works. Here is what they usually say…

 

1) The user types in http://examplelink.com , for some reason not shortened with example .com/mylink . This is just one possibility, but let's pretend it happens.

2) When they hit enter on the keyboard, the HTTP GET request is sent to whatever web server powers examplelink.com . The string in the URL is typically not changed when it reaches your server, so GET /mylink will be delivered to your website just as if you had typed that into a browser yourself.

3) Your website then takes that link and looks for a static file called /mylink , which does exist because this was set up with a 301 redirect from an unshortened version of mylink= to a shortened version. This means there is a different version of mylink= already stored in your site's files so it knows how to reroute requests for http://examplelink.com/myLink .

4) That static file is returned in the HTTP response to the browser, which loads it and sends back all of the content.

5) The browser reads that code and begins to parse out useful bits like "mylink=http://bitly.com/abcdef" .

6) The link is then presented to the user. They may decide they want to click on it or copy-and-paste onto another site that supports full URLs (like Google Search). If they choose to copy-and-paste, they are prompted by their system with what should be used as the destination URL, either bitly.com or shortening service's internal shortened domain address (e.g., v6xlsd for bitly). The user can either choose to use one or the other and will be directed accordingly.

 

Many people think that’s all there is to it, but they don’t know what goes on “in between.” This article will go into more detail about how URL shortening actually works, by way of example…

1) When the user enters http://examplelink.com , you (the website owner) get a request for /myLink . This is your chance to dynamically change which file you send back based on that URL! If we were using PHP as an example for our web server technology that would mean we could do something like this in our index.php file:

<?php $my_long_url = $_GET['my_long_url']; if(($my_long_url) && (strlen($my_long_url) > 23)) { $newlink = strtolower($my_long_url); //Not all URL shortening services use lowercase, so check for this possibility.

//Now check to see what service you are using and create a matching shortened link.

$shortenedLink = "http://bitly.com/".$newlink;

header('Location: '.$shortenedLink);

exit(); } else { header('Location: /'.$my_long_url);

exit(); } ?>The example above is extremely simplified because it only takes one parameter from the URL, but it should give you a basic idea of what URL shorteners are doing. In short, they have to have a list of shortened URLs and their destination URLs so they can redirect requests for any given link.

2) If this is a normal request that you receive often, then you likely have compiled this information into a static file on your server. You can name these files whatever you want, just keep in mind that the filename will be the first part of how your service manages its shortened URLs. Once again using PHP as an example, we could make our list of links look like this:

$urls = array( 'http://bitly.com/abcdef' => 'http://example.com/', 'http://bitly.com/xyz' => 'http://example2.com/', );

3) Now that we know how to convert a user's long URL into a shortened link and where that shortened link will go, all we need to do is find the right file and send it back as part of our HTTP response:

<?php $my_long_url = $_GET['my_long_url']; if(($my_long_url) && (strlen($my_long_url) > 23)) { $newlink = strtolower($my_long_url); //Not all URL shortening services use lowercase, so check for this possibility.

We are social