Others have answered how the redirects work but you should also know how they generate their tiny urls. You’ll mistakenly hear that they create a hash of the URL in order to generate that unique code for the shortened URL. This is incorrect in most cases, they aren’t using a hashing algorithm (where you could potentially have collisions).
Most of the popular URL shortening services simply take the ID in the database of the URL and then convert it to either Base 36 [a-z0-9] (case insensitive) or Base 62 (case sensitive).
A simplified example of a TinyURL Database Table:
ID URL VisitCount
1 www.google.com 26
2 www.stackoverflow.com 2048
3 www.reddit.com 64
...
20103 www.digg.com 201
20104 www.4chan.com 20
Web Frameworks that allow flexible routing make handling the incoming URL’s really easy (Ruby, ASP.NET MVC, etc).
So, on your webserver you might have a route action that looks like (pseudo code):
Route: www.mytinyurl.com/{UrlID}
Route Action: RouteURL(UrlID);
Which routes any incoming request to your server that has any text after your domain www.mytinyurl.com to your associated method, RouteURL. It supplies the text that is passed in after the forward slash in your URL to that method.
So, lets say you requested: www.mytinyurl.com/fif
“fif” would then be passed to your method, RouteURL(String UrlID). RouteURL would then convert “fif” to its base10 equivalent, 20103, and a database request will be made to redirect to whatever URL is stored under the ID 20103 (in this case, www.digg.com). You would also increase the visit count for Digg by one before redirecting to the correct URL.
This is a really simplified example but you should be able to get the general idea.
-
Good additions, esp. the part on the encoding. – Abel Oct 13 ’09 at 21:00
-
1really its that easy. I hate when I don’t think of the simple things. – Nathan Feger Dec 19 ’09 at 0:48
-
1
-
10Thanks for the nice explanation. So what happens when someone tries to create a short URL for an already existing long URL? Do they perform a full text search on the database? I do not think so as it will be too much time consuming. Hash or message digest based approach looks more practical. – Piyush Kansal Oct 2 ’13 at 7:13
-
-
@PiyushKansal you could use the hash internally to do a
O(1)lookup to find duplicates; and then route the existing tiny URL for that, or could choose to generate a new one. As far as I can tell,goo.glreuses the tiny urls for the same URL; try this on your end for this page: Do you get this >>goo.gl/8gVb8X? – Kingz Jun 4 ’18 at 16:29 -
As an extension to @A Salcedo answer:
Some url shortening services (Tinyarro.ws) go to extreme by using Unicode (UTF-8) to encode characters in shortened url – which allows higher amount of websites before having to add additional symbol. Since most of UTF-8 is accepted for use ((IRI) RFC 3987 handled by most browsers) that bumps from
62sites per symbol to ~1,112,064.To put in perspective one can encode 1.2366863e+12 sites with 2 symbols (
1,112,064*1,112,064) – in November 2009, shortened links onbit.lywere accessed2.1billion times (Around that time, bit.ly and TinyURL were the most widely used URL-shortening services.) which is ~600 times less than you can fit in just 2 symbols, so for full duration of existence of all url shortening services it should last another 20 years minimum till adding third symbol.