Let's talk about short links

2022.09.16
Let's talk about short links
Due to the uncertainty of open source projects, we have to build a set of short link services by ourselves to meet the needs of use. One is easy to maintain, and the other is flexible expansion. Next, analyze the code.

​Foreword

Recently, I am working on a promotion system, and I will share the long and short links involved here. The main way of promotion is to condolence to customers through text messages and push promotional links (non-advertising), but the links are really too long. Let’s not talk about the issue of charging by the number of words for text messages, but I want to delete them immediately when I see them. So the organization arranged to study how to make the links shorter and more streamlined. . .

About long and short links

  • Long link: As the name implies, it is the full URL address of the webpage. Click to jump to the webpage for content browsing.
  • Short link: It is to convert a long link into a URL address with a smaller length after processing. For example, https://sourl.cn/upNbxj is a long link https://blog.csdn.net/qq_39486758/article/details/126602389 The result after processing.
  • Compared with long links, short links will be shorter, which is convenient for dealing with problems such as character length restrictions on some third-party platforms. Of course, for Xiaobian, it can save a lot of SMS fees. Whether you can "promote and make a fortune" depends on it ~~

The principle of long and short links

  • When we enter a short link on the website, DNS will resolve the IP address of the link (that is, the short link server), and then DNS forwards the request (HTTP GET) to the short link server, and exchanges the corresponding full URL address through the short link code, and finally the short link The server redirects to the full URL address through the request (HTTP 301), and the parsing is completed. You can refer to the timing diagram:

picture

Note: Short link jumps to long links can use 301 (permanent redirection) or 302 (temporary redirection), the difference is the management of resources, 301 will permanently remove the old resources and replace them with new redirected resources ; and 302 will still retain the old resource, just redirect to the new resource, and will not be replaced, nor will the new resource be saved.

Demonstration case

  • Free online tools:

Home of the webmaster: https://tool.chinaz.com/tools/dwz.aspx, you need to register to use it.

Short URL: https://www.dwz.lc/, which provides very complete functions such as setting the validity period, access password, etc., which is convenient to use

  • Self-developed short link service: Due to the uncertainty of open source projects, we have to build a set of short link services by ourselves to meet the needs of use. One is easy to maintain, and the other is flexible expansion. Next, analyze the code.

The first is the algorithm tool class for generating short link codes. The algorithm is not fixed. You can use other algorithms to generate them according to your own habits or work requirements. The most important thing is to ensure the uniqueness of short link codes. The next step is to maintain the relational mapping of short links. Here, the editor uses set variables. It is recommended to use databases such as Mysql to persist relational data to avoid data loss and access failure.

/**
     * 解码重定向
     *
     * @param url 原始链接的编码
     * @return 重定向
     */
    @GetMapping("/redirect/{url}")
    public ModelAndView redirect(@PathVariable String url) {
        long id = BasetUtil.encode62to10(smartUrl);
        String originUrl = urlMap.get(id);
        RedirectView redirectView=new RedirectView(originUrl);
        // 301永久重定向,避免网络劫持
        redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
        return new ModelAndView(redirectView);
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

Simulation operation process: start the short link service locally, then start a business service as a long link service, generate a short link from the long link, then access the short link and successfully jump to the long link address. Demonstration results

picture

picture

picture

Summarize

The above is all the content shared in this article. Of course, there is more than one implementation method. Those who have ideas can discuss it by private message.