厘短链
使用这个网址生成短连接,比如:
原链接
短连接
把这个Url输入到浏览器,以下是请求过程
DNS首先解析获得 dlj.li 的IP地址;
然后发起HTTP GET请求,查询 zCFb4;
https://dlj.li服务器就会把请求通过HTTP 301转到对应的原链接 ;
HTTP/1.1 301 Moved Permanently\r\n [Expert Info (Chat/Sequence): HTTP/1.1 301 Moved Permanently\r\n] Response Version: HTTP/1.1 Status Code: 301 [Status Code Description: Moved Permanently] Response Phrase: Moved Permanently Date: Fri, 22 Jul 2022 09:04:42 GMT\r\n Transfer-Encoding: chunked\r\n Connection: keep-alive\r\n Cache-Control: max-age=3600\r\n Expires: Fri, 22 Jul 2022 10:04:42 GMT\r\n Location: https://dlj.li/404\r\n [truncated]Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=iJMIJ0Z5jjADPMhHYaGyyEeZ5XY7SMzuPuolAYfLXrDfYip4FNLGc6MAQEXrJ4nzKGCnpa6oVMOlg%2B3hgf8Rgtz0kILPv2mJxvPaZy2aeOJy%2F0p6gt7Mhkg%3D"}],"group":"cf-nel"," NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}\r\n Vary: Accept-Encoding\r\n Server: cloudflare\r\n CF-RAY: 72eb02ac6b767d7a-LAX\r\n alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400\r\n \r\n [HTTP response 1/1] [Time since request: 0.191952000 seconds] [Request in frame: 77] [Request URI: http://dlj.li/404] HTTP chunked response File Data: 0 bytes
后面的解析过程就和平常网址解析是一样的了。
缩短URL的实现有很多,介绍一种最简单的(其他也大同小异)
在数据建一张表(自增ID),存入原URL,生成的ID直接拼接到服务器域名后部;
只要保证这个ID在表中唯一(使用其他方式也可以);
理论上5位长度假设一位上可以用52个字符,那就是可以存52的5次方是380,204,032个短URL,实际肯定比这个多。