URL 缩短器如何工作?

发布于 2024-10-10 01:57:06 字数 124 浏览 9 评论 0原文

我想知道 URL 缩短器是如何工作的,例如它们如何从地址栏中提取文本并将其映射到正确的 URL,然后重定向它。他们使用什么编程语言?他们如何维护映射历史?他们如何确保缩短的网址的唯一性?外行人如何在不访问 URL 的情况下取消映射呢?

I wonder how a URL Shortener works, like how they extract the text from address bar and map it to correct URL, later redirect it. What programming language do they use? How do they maintain the history of the mapping? How do they ensure the uniqueness of the shortened url? How can a lay man unmap it without visiting the URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ弥猫深巷。 2024-10-17 01:57:06

Wiki Is Your Friend

基本上,名称较短的网站用作占位符,例如位.ly。

然后,bit.ly 生成一个密钥供用户提供,该密钥是随机生成的,不会重复。使用 35 个字符选项和 8 个左右的值,进行数学计算。这是很多可能的键。如果 URL 等于以前存在的密钥,我记得在某处读到过它们也会重用密钥。

它们并不真正使用特定的编程语言,只是使用简单的 URL 重定向,这可以通过 HTTP 响应状态代码 301、302、307 或 308 来完成,具体取决于具体情况。

Wiki Is Your Friend

Basically, a website with a shorter name is used as a place holder, such as bit.ly.

Then, bit.ly generates a key for the user to provide, which is randomly generated to not repeat. With 35 character options and 8 or so values, do the math. That's a lot of possible keys. If a URL is equal to a previously existing key, I remember reading somewhere that they reuse keys as well.

They don't really use a specific programming language, they just use a simple URL redirect, which can be done with HTTP response status code 301, 302, 307 or 308, depending.

寄居人 2024-10-17 01:57:06

URL 缩短程序只是生成一个短代码,将目标 URL 映射到该短代码,并提供一个新的 URL。访问 URL 会以短代码为键执行数据库查找,并将您重定向到目标 URL。缩短的 URL 和目标 URL 之间不存在算法关联,因此如果不通过 URL 缩短器的系统,您就无法“取消映射”它。

您可以使用任何编程语言和数据存储来完成此操作。代码生成也很简单,可以确保唯一性;如果您有一个递增的主整数密钥,您可以简单地将密钥编码为 base62 并提供服务。由于代码本质上是增量的,因此永远不会发生冲突。

URL shortners just generate a shortcode, map the target URL to the shortcode, and provide a new URL. Visiting the URL performs a database lookup with the shortcode as a key, and redirects you to the target URL. There is no algorithmic association between a shortened URL and a destination URL, so you can't "unmap" it without going through the URL shortener's systems.

You can do it with any programming language and data store. Code generation is trivial to ensure uniqueness as well; if you had an incrementing primary integer key, you could simply encode the key as base62 and serve that. Since codes are incremental in nature, you'll never have a conflict.

超可爱的懒熊 2024-10-17 01:57:06

实际上,该过程非常简单:有一个脚本请求 URL,生成一个随机字符串(并验证该字符串尚未使用),然后将这两个字符串放入某种数据库中。当您请求 URL 时,另一个脚本会在数据库中查找随机字符串,如果找到则将您重定向到该站点。

当然,由于需要诸如滥用防护、URL 过滤、垃圾邮件防护、URL 验证等功能,这在生产中会更加复杂。但这些实现起来非常简单。


语言无关紧要,大多数人都可以。

The process is pretty simple actually: There a script that asks for the URL, generates a random string (and verifies that this string isn't already used), and puts the two in some kind of database. When you request a url, another script looks in the database for the random string, and if its found redirects you to the site.

This is of course more complicated in production due to needed features like abuse prevention, URL filtering, spam prevention, URL verification, etc. But those are pretty simple to implement.


The language is irrelevant, mostly any one will do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文