开发 URL 缩短器

发布于 2024-09-29 21:07:15 字数 111 浏览 2 评论 0原文

我正在尝试开发一个 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 技术交流群。

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

发布评论

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

评论(6

仙女山的月亮 2024-10-06 21:07:16
  1. 我不明白如何为每个长 URL 创建唯一的字符串以用作短 URL。就像其他流行的 URL 缩短程序一样。
    正如 sugerman 所说,这很简单,您只需创建一个哈希表即可。

  2. 我该如何做到这一点?
    有多种动态方法可以做到这一点,但最简单、最有效的方法是在数据库中有一个 2 字段表,其中包含哈希键和完整 url。然后您的服务器(例如 Apache)将能够重定向到正确的页面。

  3. 是否可以使所有短网址具有相同的长度?
    是的,在某种程度上,但是一旦达到密钥的最大数量,您将不得不重用/替换短网址 ID。当您设置固定长度时,您就限制了可能性。

我向你提出的问题:

我假设URL缩短器你指的是类似的东西jsFiddle 或 Pastebin,因为它们有类似 http://jsfiddle.net/sdfj2/ 的东西。否则,我们需要更多澄清。

  1. 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.

  2. 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.

  3. 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 like http://jsfiddle.net/sdfj2/. Otherwise, we'd need some more clarification.

萧瑟寒风 2024-10-06 21:07:16

您可能想要创建一个简单的数据库表,将短值映射到 URL。

您可以生成的最简单的短网址只是一个序列号或自动增量列(分配第一个值 1,然后是 2,依此类推)

可以使所有 URL 的长度相同,直到您用完为止相同长度的值,例如,如果您仅使用数字(作为一个简单的示例),则它将是从 0000 到 9999。

神奇的部分是您可以使用 mod_rewrite 将 url 作为参数传递给脚本,您的应用程序在数据库中查找值,然后重定向用户。

mod_rewrite 的重写规则将采用像 example.com/0000 这样的 URL 并重定向到 example.com/index.py?id=0000 您可以将其放在 .htaccess 中(我假设您正在使用 apache。)

您的应用程序只是读取 id 并重定向到关联的页面。

RewriteRule ^([0-9]+)/?$ index.py?id=$1 [QSA,L]

如果您决定使用哈希值或简单的 Base64 序列号(这将是更紧凑的自动增量),它看起来会略有不同:

RewriteRule ^(.*)/?$ index.py?id=$1 [QSA,L]

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.

RewriteRule ^([0-9]+)/?$ index.py?id=$1 [QSA,L]

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:

RewriteRule ^(.*)/?$ index.py?id=$1 [QSA,L]
我不在是我 2024-10-06 21:07:16

我写了上面链接的博客文章。基本上,我的做法如下:

将长网址存储在带有自动递增索引字段的表中。检索索引。将其转换为字符串,就像将其转换为十六进制或八进制数字一样,但使用所有可用字符作为可能的“数字”。例如,假设我们有 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.

不奢求什么 2024-10-06 21:07:16

您可能需要设置一个哈希函数来生成缩短的网址。

编辑:

与其坐在这里为您总结一篇非常详细的维基百科文章,这里有一个博客文章的链接,解释了哈希函数如何与您的确切主题(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).

残花月 2024-10-06 21:07:16

检查 django-shorturls 应用程序。

Check django-shorturls app.

一江春梦 2024-10-06 21:07:16

这是我的看法: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.

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