URL缩短:最好的编码方式? [英] URL shortener: best encoding method?

查看:575
本文介绍了URL缩短:最好的编码方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个链接缩短服务,我正在使用增加的ID字段的base64编码/解码来创建我的网址。 ID为6的网址为: http://mysite.com/Ngw

I'm creating a link shortening service and I'm using base64 encoding/decoding of an incremented ID field to create my urls. A url with the ID "6" would be: http://mysite.com/Ng==

我还需要允许用户创建自定义的网址名称,例如 http ://mysite.com/music

I need to also allow users to create a custom url name, like http://mysite.com/music

到目前为止,这是我的(可能是错误的)方法。帮助修复它将不胜感激。

Here's my (possibly faulty) approach so far. Help in fixing it would be appreciated.

当有人创建一个新的链接:

When someone creates a new link:


    <我从数据库中获取最大的链接ID(不是自动递增)
  • 将ID增加1

  • 生成一个短的URL代码 http://website.com/?short url name])由base64_encoding该ID

  • 插入链接表:id,short_url_code,destination_url

  • I get the largest link ID from the database (it's not auto incremented)
  • Increment the ID by 1
  • Generate a short URL code (http://website.com/[short url name]) by base64_encoding that ID
  • Insert into links table: id, short_url_code, destination_url

当有人创建一个新链接并传递自定义的短网址:

When someone creates a new link and passes a custom short URL:


  • 我的计划是base64_decode他们的自定义字符串,并将其用作链接ID,但我没有意识到你不能只是base64_decode任何字母数字字符串,并将其转换成一个数字。

有没有更好的编码方法,让我把任何数字变成短字符串和任何字符串到一个数字,所以我可以随时查找短的URL(不管是自定义还是自动生成)通过转动将名称转换成一个号码,并查询一个ID等于该号码的链接?

Is there a better encoding method that will let me turn any number into a short string, and any string into a number, so I can always lookup short urls (whether custom or autogenerated) by turning the name into a number and querying for a link with an ID equal to that number?

推荐答案

首先,确保您在 ID short_url_code 列之间存在中的单一限制。

First and foremost, make sure you have unicity constraints in place on the ID and short_url_code columns.

当有人创建一个新链接时:

When someone creates a new link:


  1. 获取下一个最大的链接 ID 从数据库(出于性能原因,您应该真正重用 autoincrement SEQUENCE ,具体取决于您的RDBMS提供的内容;否则继续选择 MAX(ID)+1

  2. 生成一个简短的URL代码( http: / code>)从 ID 使用 base64_encode 其他自定义或标准编码方案

  3. 插入链接表: ID,short_url_code,destination_url

  4. 如果插入因违反约束而失败,请返回步骤1尝试新的 ID ;您可能已经违规了:

  1. Get the next largest link ID from the database (for performance reasons you should really REALLY use autoincrement or SEQUENCE, depending on what your RDBMS offers; otherwise go ahead and select MAX(ID)+1 )
  2. Generate a short URL code (http://website.com/[short url name]) from ID using base64_encode or any other custom or standard encoding scheme
  3. Insert into the links table: ID, short_url_code, destination_url
  4. If the insert fails because of a constraint violation go back to step 1 to try a new ID; you may have had a violation because:


  1. 同一个ID已经被另一个线程/进程等同地使用(即插入) (如果您使用 autoincrement SEQUENCE ,则可能会发生这种情况,并且可能会发生和/或

  2. 相同的 short_url_code 已经被用作自定义的URL(这将很少发生,除非有人是尝试在您的网站上造成麻烦)

  1. the same ID has already been used (i.e. inserted) in parallel by another thread/process etc. (this will not happen if you used autoincrement or SEQUENCE, and may happen quite often otherwise), and/or
  2. the same short_url_code has already been used as a custom URL (this will happen very seldomly unless someone is trying to cause trouble on your site)


  • 如果插入成功,提交并将短URL返回给用户


  • 当有人创建新的链接并传递自定义的短网址时:

    When someone creates a new link and passes a custom short URL:


    1. 执行与上述相同的步骤1

    2. 而不是从 ID中生成短网址部分如上面的步骤2,使用用户提供的自定义 short_url_code

    3. 执行与上述相同的步骤3

    4. 如果e插入失败是因为:

    1. Perform the same step 1 as above
    2. Instead of generating the short URL part from ID as in step 2 above, use the custom short_url_code provided by the user
    3. Perform the same step 3 as above
    4. If the insert failed because of:

    1. ID上的约束违规:返回步骤1尝试 ID

    2. short_url_code 上的约束违规:返回错误用户要求他选择不同的自定义网址,因为他/她提供的短URL已被使用

    1. a constraint violation on ID: go back to step 1 to try a new ID
    2. a constraint violation on short_url_code: return an error to the user asking him to pick a different custom URL, as the short URL he/she provided has already been used


  • 执行相同的步骤5以上

  • 这篇关于URL缩短:最好的编码方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆