小网址如何运作
我一直想知道小网址是如何工作的。
我想为我的网站开发类似的东西,但和大多数人一样,我使用 GUID 作为 id。创建对象后,我应该生成一个 10 个字符的随机字符串来用作公共 id,还是有更聪明的方法?
旧网址示例:www.mysite.com/default.aspx?userId={id}
新网址示例:www.mysite.com/pwzd4r9niy
I have been wondering how tiny url works.
I would like to develop something similar for my site, but as most people, I use GUIDs for ids. When an object is created, should I then generate a 10 character random string to use as public id, or is there a smarter approach?
Example of old url: www.mysite.com/default.aspx?userId={id}
Example of new url: www.mysite.com/pwzd4r9niy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您可以使用任何类型的随机字符串生成器或 GUID。我认为没有更明智的方法。 (Palantir 提供了一个很好的替代方案,对传入的 URL 进行哈希处理。)
其余部分相对简单:保留包含 ID 和目标 URL 的数据库表;当请求传入时,查找 ID 并执行
header
重定向到目标 URL。更多讨论请参见这篇博文。
现在还有使用字典列表中的单词来构建 URL 的重定向服务。
遗憾的是,EvilURL 消失了!它过去常常创建“短”URL,
这是唯一真正有价值的 URL 重定向服务。 :)
并且,作为一点琐事,
http://to
是最短的重定向服务(而且,我认为是人类已知的最短的网址)。You can use any kind of random string generator or GUID for this. I don't think there is a much smarter approach. (Palantir offers a nice alternative though, hashing the incoming URL. )
The rest is relatively straightforward: Keep a database table with IDs and target URLs; When a request comes in, look up the ID and do a
header
redirect to the target URL.More discussion in this blog post.
There also are redirection services out there now that use words from a dictionary list to build a URL.
Sadly, EvilURL is gone! It used to create "short" URLs like
that was the only URL redirection service really worthwhile. :)
And, as a bit of trivia,
http://to
is the shortest redirection service (and, I think the shortest web URL) known to man.只需将整个字符串散列到合理的长度即可。
Just hash the entire string, to a reasonable length.