URL 缩短程序是如何工作的?

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

39199 次浏览

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.

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.

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.