开发 URL 缩短器
我正在尝试开发一个 URL 缩短器应用程序来练习 Django。我不明白如何为每个长 URL 创建唯一的字符串以用作短 URL。就像其他流行的 URL 缩短器一样。我该怎么做?是否可以使所有短网址的长度相同?
I am trying to develop a URL shortener application for practice with Django. I do not understand how can I create unique strings for each long URL to use as a short URL. Something like other popular URL shorteners do. How can I do this? Is it possible to make all short urls of the same length?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不明白如何为每个长 URL 创建唯一的字符串以用作短 URL。就像其他流行的 URL 缩短程序一样。
正如 sugerman 所说,这很简单,您只需创建一个哈希表即可。
我该如何做到这一点?
有多种动态方法可以做到这一点,但最简单、最有效的方法是在数据库中有一个 2 字段表,其中包含哈希键和完整 url。然后您的服务器(例如 Apache)将能够重定向到正确的页面。
是否可以使所有短网址具有相同的长度?
是的,在某种程度上,但是一旦达到密钥的最大数量,您将不得不重用/替换短网址 ID。当您设置固定长度时,您就限制了可能性。
我向你提出的问题:
我假设
URL缩短器
你指的是类似的东西jsFiddle 或 Pastebin,因为它们有类似http://jsfiddle.net/sdfj2/
的东西。否则,我们需要更多澄清。I do not understand how can I create unique strings for each long URL to use as a short URL. Something like other popular URL shorteners do.
As sugerman has said, this is simple, you just create a hash table.
How can I do this?
There are dynamic ways to do this, but the simplest and most effective is to have a 2 field table in a database, which holds the hashkey and full url. Then your server, like Apache, would have the ability to redirect to the correct page.
Is it possible to make all short urls of the same length?
Yes, to a certain extent, however once you reach the maximum amount of keys, you would have to reuse/replace the short url IDs. When you set a fixed-length, then you're limiting the amount of possibilities.
My question to you:
I'm under the assumption that by
URL shortener
you are referring to something like jsFiddle or a pastebin in that they have something likehttp://jsfiddle.net/sdfj2/
. Otherwise, we'd need some more clarification.您可能想要创建一个简单的数据库表,将短值映射到 URL。
您可以生成的最简单的短网址只是一个序列号或自动增量列(分配第一个值 1,然后是 2,依此类推)
可以使所有 URL 的长度相同,直到您用完为止相同长度的值,例如,如果您仅使用数字(作为一个简单的示例),则它将是从 0000 到 9999。
神奇的部分是您可以使用 mod_rewrite 将 url 作为参数传递给脚本,您的应用程序在数据库中查找值,然后重定向用户。
mod_rewrite 的重写规则将采用像 example.com/0000 这样的 URL 并重定向到 example.com/index.py?id=0000 您可以将其放在 .htaccess 中(我假设您正在使用 apache。)
您的应用程序只是读取 id 并重定向到关联的页面。
如果您决定使用哈希值或简单的 Base64 序列号(这将是更紧凑的自动增量),它看起来会略有不同:
You'll probably want to create a simple db table mapping a short value to a URL.
The simplest short url you can generate would just be a serial number or autoincrement column (assign the first value 1, then 2 and so on)
It's possible to make all of the URLs of the same length as long until you don't run out of values of the same length, eg if you were using only numbers (as a simple example) it would be from 0000 to 9999.
The magic part would be where you would use mod_rewrite to pass the url to your script as a parameter, have your application look the value up in the db then redirect the user.
This rewrite rule for mod_rewrite will take a URL like example.com/0000 and redirect to example.com/index.py?id=0000 You would put this in .htaccess (I'm assuming you're using apache.)
Your application just reads the id and redirects to the associated page.
If you decide to go with a hash, or a simple base64 serial number (which would be a more compact autoincrement), it would just look slightly different:
我写了上面链接的博客文章。基本上,我的做法如下:
将长网址存储在带有自动递增索引字段的表中。检索索引。将其转换为字符串,就像将其转换为十六进制或八进制数字一样,但使用所有可用字符作为可能的“数字”。例如,假设我们有 az、AZ 和 0-9 可用,请执行以下操作:
0 = 0
1 = 1
...
9 = 9
10 = 一个
11 = b
...
35 = z
36 = 一个
37 = 乙
...
60 = Z
61 = 10
62 = 11
...
70 = 19
71 = 1a
72 = 1b
你明白规律了吗?它是一种从 10 基数到 n 基数的通用转换算法,其中 n 是您可以使用的字符数。
I wrote the blog post linked above. basically, how I did it was as follows:
Store the long url in a table with an auto-incrementing index field. Retrieve the index. Convert it to a string, the same way you would convert it to say, a hexadecimal or octal number, but using all available characters as possible "digits". For example, say we have a-z, A-Z, and 0-9 available, do the following:
0 = 0
1 = 1
...
9 = 9
10 = a
11 = b
...
35 = z
36 = A
37 = B
...
60 = Z
61 = 10
62 = 11
...
70 = 19
71 = 1a
72 = 1b
You get the pattern? It's a generic conversion algorithm from a base-10 number to a base-n number, where n is the number of characters to your disposal.
您可能需要设置一个哈希函数来生成缩短的网址。
编辑:
与其坐在这里为您总结一篇非常详细的维基百科文章,这里有一个博客文章的链接,解释了哈希函数如何与您的确切主题(URL 缩短器)相关。
You're probably going to want to set up a hash function to generate the shortened URL.
Edit:
Rather than sit here and summarize a pretty detailed Wikipedia article for you, here's a link to a blog post explaining how hash functions work in relation to your exact topic (URL shorteners).
检查 django-shorturls 应用程序。
Check django-shorturls app.
这是我的看法:https://github.com/bitmazk/django-tinylinks
一位客户一年来一直在生产中使用它。
Here is my take on it: https://github.com/bitmazk/django-tinylinks
One client is using this in production since one year.