Short URL link principle

Source: Internet
Author: User

First, what is a short link

Meaning: The ordinary URL is converted into a relatively short URL. For example: Http://t.cn/RlB2PdD, such as Weibo, which is used in the application of the word limit.

Advantages: Short, less characters, beautiful, easy to publish, spread.

Baidu Short URL: http://dwz.cn/

Google Short URL: https://goo.gl/

Second, the principle analysis

When we enter Http://t.cn/RlB2PdD in the browser, we pass through the following steps:

1. The browser resolves DNS to obtain the IP address of the domain name;

2. When the IP is acquired, an HTTP GET request is sent to this IP address to obtain the long link address of the RLB2PDD;

3.HTTP through 301 to the corresponding long link url;

Note: Why use 301 here? 301 is a permanent transfer (redirect), that is, the short address once generated will not change, so it conforms to the HTTP 301 semantics;

Third, the implementation of the algorithm

The short address is generally made up of the 62 characters of the 0-9a-za-z, so we can generate a 62-binary string, which can be converted to 62 binary number according to the 10 binary number, for example, when we store the database, we can generate the corresponding 62 decimal code according to the ID. As the number of short links, the longest 6-bit, the shortest 1-bit, the following is the method of implementation for reference only:

/** * 10 binary to 62 binary * @param $num * @return string*/functionFROM10_TO62 ($num) {    $to= 62;//0-9a-za-z total of 62 characters    $dict= ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';//This can disrupt the sequence .    $ret= ' ';  Do {        $ret=$dict[$num%$to] .$ret;//Take the mold        $num= Floor($num/$to);//take the remainder} while($num> 0); return $ret;}/** * 62 binary to 10 * @param $num * @return int|string*/functionFrom62_to10 ($num){    $from= 62; $num=Strval($num); $dict= ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; $len=strlen($num); $dec= 0;  for($i= 0;$i<$len;$i++) {        $pos=Strpos($dict,$num[$i]);//find where a character appears        $bcpow=Bcpow($from,$len-$i-1);//take 62 n (reserved integers)        $bcmul=Bcmul($bcpow,$pos);//calculates 10 binary values for each 62 binary character        $dec=Bcadd($bcmul,$dec);//calculated values are accumulated    }    return $dec;}Var_dump(FROM10_TO62 (1000));Var_dump(From62_to10 (' G8 '));

Content reference from: https://hufangyun.com/2017/short-url/

Short URL link principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.