{"id":77,"date":"2019-04-17T18:15:03","date_gmt":"2019-04-17T18:15:03","guid":{"rendered":"https:\/\/r0b3rt.com\/andr-web\/?p=77"},"modified":"2019-04-17T18:15:03","modified_gmt":"2019-04-17T18:15:03","slug":"url-how-to","status":"publish","type":"post","link":"http:\/\/r0b3rt.com\/andr-web\/url-how-to\/","title":{"rendered":"URL How-to"},"content":{"rendered":"<p>Others have answered how the redirects work but you should also know how they generate their tiny urls. You&#8217;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&#8217;t using a hashing algorithm (where you could potentially have collisions).<\/p>\n<p>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).<\/p>\n<p>A simplified example of a TinyURL Database Table:<\/p>\n<pre><code>ID       URL                           VisitCount\r\n 1       www.google.com                        26\r\n 2       www.stackoverflow.com               2048\r\n 3       www.reddit.com                        64\r\n...\r\n 20103   www.digg.com                         201\r\n 20104   www.4chan.com                         20\r\n<\/code><\/pre>\n<p>Web Frameworks that allow flexible routing make handling the incoming URL&#8217;s really easy (Ruby, ASP.NET MVC, etc).<\/p>\n<p>So, on your webserver you might have a route action that looks like (pseudo code):<\/p>\n<pre><code>Route: www.mytinyurl.com\/{UrlID}\r\nRoute Action: RouteURL(UrlID);\r\n<\/code><\/pre>\n<p>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.<\/p>\n<p>So, lets say you requested: www.mytinyurl.com\/fif<\/p>\n<p>&#8220;fif&#8221; would then be passed to your method, RouteURL(String UrlID). RouteURL would then convert &#8220;fif&#8221; 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.<\/p>\n<p>This is a really simplified example but you should be able to get the general idea.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li id=\"comment-1421978\" class=\"comment js-comment \" data-comment-id=\"1421978\">\n<div class=\"comment-text js-comment-text-and-form\">\n<div class=\"comment-body js-comment-edit-hide\"><span class=\"comment-copy\">Good additions, esp. the part on the encoding.<\/span> \u2013 <a class=\"comment-user\" title=\"42,721 reputation\" href=\"https:\/\/stackoverflow.com\/users\/111575\/abel\">Abel<\/a> <span class=\"comment-date\" dir=\"ltr\"><a class=\"comment-link\" href=\"https:\/\/stackoverflow.com\/questions\/1562367\/how-do-short-urls-services-work#comment1421978_1562793\"><span class=\"relativetime-clean\" title=\"2009-10-13 21:00:10Z\">Oct 13 &#8217;09 at 21:00<\/span><\/a><\/span><\/div>\n<\/div>\n<\/li>\n<li id=\"comment-1840181\" class=\"comment js-comment \" data-comment-id=\"1840181\">\n<div class=\"js-comment-actions comment-actions\">\n<div class=\"comment-score js-comment-edit-hide\"><span class=\"cool\" title=\"number of 'useful comment' votes received\">1<\/span><\/div>\n<\/div>\n<div class=\"comment-text js-comment-text-and-form\">\n<div class=\"comment-body js-comment-edit-hide\"><span class=\"comment-copy\">really its that easy. I hate when I don&#8217;t think of the simple things.<\/span> \u2013 <a class=\"comment-user\" title=\"14,418 reputation\" href=\"https:\/\/stackoverflow.com\/users\/8563\/nathan-feger\">Nathan Feger<\/a> <span class=\"comment-date\" dir=\"ltr\"><a class=\"comment-link\" href=\"https:\/\/stackoverflow.com\/questions\/1562367\/how-do-short-urls-services-work#comment1840181_1562793\"><span class=\"relativetime-clean\" title=\"2009-12-19 00:48:01Z\">Dec 19 &#8217;09 at 0:48<\/span><\/a><\/span><\/div>\n<\/div>\n<\/li>\n<li id=\"comment-9696363\" class=\"comment js-comment \" data-comment-id=\"9696363\">\n<div class=\"js-comment-actions comment-actions\">\n<div class=\"comment-score js-comment-edit-hide\"><span class=\"cool\" title=\"number of 'useful comment' votes received\">1<\/span><\/div>\n<\/div>\n<div class=\"comment-text js-comment-text-and-form\">\n<div class=\"comment-body js-comment-edit-hide\"><span class=\"comment-copy\">what is a good heuristic to generate random shortened urls?<\/span> \u2013 <a class=\"comment-user\" title=\"5,305 reputation\" href=\"https:\/\/stackoverflow.com\/users\/367319\/saher-ahwal\">Saher Ahwal<\/a> <span class=\"comment-date\" dir=\"ltr\"><a class=\"comment-link\" href=\"https:\/\/stackoverflow.com\/questions\/1562367\/how-do-short-urls-services-work#comment9696363_1562793\"><span class=\"relativetime-clean\" title=\"2011-10-28 23:36:20Z\">Oct 28 &#8217;11 at 23:36<\/span><\/a><\/span><\/div>\n<\/div>\n<\/li>\n<li id=\"comment-28293506\" class=\"comment js-comment \" data-comment-id=\"28293506\">\n<div class=\"js-comment-actions comment-actions\">\n<div class=\"comment-score js-comment-edit-hide\"><span class=\"warm\" title=\"number of 'useful comment' votes received\">10<\/span><\/div>\n<\/div>\n<div class=\"comment-text js-comment-text-and-form\">\n<div class=\"comment-body js-comment-edit-hide\"><span class=\"comment-copy\">Thanks 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.<\/span> \u2013 <a class=\"comment-user\" title=\"526 reputation\" href=\"https:\/\/stackoverflow.com\/users\/1078414\/piyush-kansal\">Piyush Kansal<\/a> <span class=\"comment-date\" dir=\"ltr\"><a class=\"comment-link\" href=\"https:\/\/stackoverflow.com\/questions\/1562367\/how-do-short-urls-services-work#comment28293506_1562793\"><span class=\"relativetime-clean\" title=\"2013-10-02 07:13:54Z\">Oct 2 &#8217;13 at 7:13<\/span><\/a><\/span><\/div>\n<\/div>\n<\/li>\n<li id=\"comment-28293506\" class=\"comment js-comment \" data-comment-id=\"28293506\">\n<div class=\"comment-text js-comment-text-and-form\">\n<div class=\"comment-body js-comment-edit-hide\"><\/div>\n<\/div>\n<\/li>\n<li id=\"comment-88379670\" class=\"comment js-comment \" data-comment-id=\"88379670\">\n<div class=\"js-comment-actions comment-actions\">\n<div class=\"comment-score js-comment-edit-hide\"><\/div>\n<\/div>\n<div class=\"comment-text js-comment-text-and-form\">\n<div class=\"comment-body js-comment-edit-hide\"><span class=\"comment-copy\">@PiyushKansal you could use the hash internally to do a <code>O(1)<\/code> 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, <code>goo.gl<\/code> reuses the tiny urls for the same URL; try this on your end for this page: Do you get this &gt;&gt; <code>goo.gl\/8gVb8X<\/code> ?<\/span> \u2013 <a class=\"comment-user\" title=\"3,265 reputation\" href=\"https:\/\/stackoverflow.com\/users\/1642266\/kingz\">Kingz<\/a> <span class=\"comment-date\" dir=\"ltr\"><a class=\"comment-link\" href=\"https:\/\/stackoverflow.com\/questions\/1562367\/how-do-short-urls-services-work#comment88379670_1562793\"><span class=\"relativetime-clean\" title=\"2018-06-04 16:29:21Z\">Jun 4 &#8217;18 at 16:29<\/span><\/a><\/span><\/div>\n<\/div>\n<div><\/div>\n<\/li>\n<li data-comment-id=\"88379670\">\n<div class=\"post-text\">\n<p>As an extension to @A Salcedo answer:<\/p>\n<p>Some url shortening services (Tinyarro.ws) go to extreme by using Unicode (UTF-8) to encode characters in shortened url &#8211; which allows higher amount of websites before having to add additional symbol. Since most of <a href=\"https:\/\/stackoverflow.com\/questions\/2742852\/unicode-characters-in-urls#answer-2744184\">UTF-8 is accepted for use<\/a> (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Internationalized_Resource_Identifier\" rel=\"nofollow noreferrer\">(IRI) RFC 3987 handled by most browsers<\/a>) that bumps from <code>62<\/code> sites per symbol to ~<code>1,112,064<\/code>.<\/p>\n<p>To put in perspective one can encode 1.2366863e+12 sites with 2 symbols (<code>1,112,064*1,112,064<\/code>) &#8211; in November 2009, shortened links on <code>bit.ly<\/code> were accessed <code>2.1<\/code> billion times (<a href=\"https:\/\/en.wikipedia.org\/wiki\/URL_shortening#History\" rel=\"nofollow noreferrer\">Around that time, bit.ly and TinyURL were the most widely used URL-shortening services.<\/a>) 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.<\/p>\n<\/div>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Others have answered how the redirects work but you should also know how they generate their tiny urls. You&#8217;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&#8217;t using a hashing algorithm (where you could &hellip; <a href=\"http:\/\/r0b3rt.com\/andr-web\/url-how-to\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;URL How-to&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-computer-it"],"_links":{"self":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":1,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":78,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts\/77\/revisions\/78"}],"wp:attachment":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}