preface

Only a bald head can be strong.

Welcome to our GitHub repository Star: github.com/ZhongFuChen…

Recently, I received a demand related to the knowledge of short links, so I went to look up the relevant information, and here I will share it with you.

I help Ali cloud to promote the server 89/ year, 229/3 years, buy to send yourself, send my girlfriend immediately New Year’s day is more appropriate, bought to build a project to see the interviewer also sweet, but also familiar with the technology stack, (old users with family accounts to buy, I use my girlfriend’s 😂). Scan or click to buy

Build tutorial, from 0 to take you step by step to build😂

I. Introduction of short links

For example, now I’m making address is this: https://github.com/ZhongFuCheng3y/3y (36)

I can convert the above address into https://dwz.cn/LwlrfG4j(23 characters through baidu’s short link service.

So why did I convert my original URL to a shorter link? For example, we send text messages to remind users to go to XXX. XXX has preferential activities, and there is often a link to jump to the copywriting, so that users can quickly go to the corresponding activity landing page.

The sending of SMS requires cost. The cost of SMS mainly consists of two aspects:

  1. The number of people who send it (the more people send it, the more it costs to text, which I won’t explain)
  2. The number of text messages sent (for example, if the text is more than 70 characters, two text messages will be charged; if the text is more than 140 characters, three text messages will be charged)

So when sending text messages to users: either put more accurate and quality users in order to control the number of messages, or try to limit the word count of the text.

Obviously, if you accompany a text message with a generic URL, there’s not much room for real text. As a result, we can find that the URL pushed by SMS messages of major companies is a short link.

For example, some platforms will limit the number of words when Posting messages, which can be easily limited if our URL is too long:

The advantages of using short links: short, less characters, beautiful, easy to publish, spread.

Two, short link it is how to do it?

Let’s go back to the generated short chain https://dwz.cn/LwlrfG4j

Although this link looks a bit strange, it is still a link, and we can tell from the characteristics of the URL:

  • dwz.cnIs the domain name
  • LwlrfG4jIs the parameter

Let’s request a short link in the browser to see what happens:

The principle of short links is:

  • Will long link through certainmethodsGenerate a short link
  • When accessing the short link, you actually visit the short link server, and then retrieve the corresponding long link according to the parameters of the short link
  • Redirect redirect

2.1 Core problems to be solved

Through the above analysis we can know is that we have to do the actual core is how to find corresponding LwlrfG4j parameters like this the complete URL:https://github.com/ZhongFuCheng3y/3y

Head first thought is: can through a compression algorithm to compress the https://github.com/ZhongFuCheng3y/3y smaller characters?

Obviously, no, most compression algorithms work for large text, and the URL itself is not necessarily large… It’s gonna be bigger than the original URL.

The second thing that comes to mind is: Can we use a Hash algorithm? You still can’t, you have Hash collisions

  • What is a hash collision? When two different strings (values) are hashed, they will have the same Hash value.
  • This means that two completely different long chains get exactly the same hash value, and my short chain relies on the hash value to find the long chain (one short chain corresponds to multiple long chains, which doesn’t make sense).

The third thing that comes to mind? Not in my head.

Now the industry used more is the number generator (ID increment)+62 base code:

  • For example, I’m going tohttps://github.com/ZhongFuCheng3y/3yAs a10000And then10000The result of base 62 coding is:2Bi

Then my short chain URL can be changed into https://3y.cn/2Bi, where 3y.cn is the domain name and 2Bi is the parameter converted into base 62.

Why use base 62 conversion? I’ve heard a lot about 64 conversion

  • Base 62 conversion because base 62 conversiononlyNumber + Lowercase + uppercase letter. The 64-bit conversion will contain/.+Such symbols (characters that do not match normal URLS)
  • Switching from base 10 to base 62 can shorten characters, and if we want six characters, there are already 56 billion combinations.

Conclusion:

  • After the ID is increased, it is converted to base 62, and the mapping relationship is saved in DB to generate a short link

3. The link of SMS directly jumps to APP

The following sources: sq.163yun.com/blog/articl… Xixi blowing snow

The sum is:

  • Through Deep Links (iOS is Universal Links), you can click the SMS link to directly evoke the App.
  • If the system does not support Deep Links for any reason, the alternative isintent filter, but a pop-up box lets the user choose which App to open the link;
  • If the user does not select our App but chooses the browser to open, passThe custom schemeTry to evoke the App;
  • Due to technical and cost issues, we ignore not supportThe custom schemeBrowser.

The last

This article is mainly a brief understanding of the relevant knowledge of short links, a complete short chain service must consider more things, here I will not expand (after all, I have not really written, you can continue to learn in the link below) ~

For more information:

  • www.zhihu.com/question/29…
  • Hufangyun.com/2017/short-…
  • Blog.csdn.net/c10WTiybQ1Y…

This has been included in my GitHub featured articles, welcome to Star: github.com/ZhongFuChen…

Happy to export dry Java technology public number: Java3y. The public account has more than 300 original technical articles, massive video resources, beautiful brain map, attention can be obtained!

Thank you very much talent can see here, if this article is written well, feel “three crooked” I have something to ask for praise for attention ️ to share 👥 to leave a message 💬 for warm male I really very useful!!

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see you in the next article!