How does tiny url work

 ̄綄美尐妖づ 提交于 2019-12-23 12:22:54

问题


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


回答1:


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

http://evilURL.com/donkey_porn-shotguns/cracking-virus-exploit

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.




回答2:


Just hash the entire string, to a reasonable length.



来源:https://stackoverflow.com/questions/3193000/how-does-tiny-url-work

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