How does a URL Shortener work? [closed]

蓝咒 提交于 2019-11-29 20:05:38

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 HTML i believe.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!