How to convert a long url to short url

已查看 4 次
跳至第一个未读帖子

Gcompany

未读,
2007年5月30日 04:24:092007/5/30
收件人 Ruby on Rails: Spinoffs
how do we convert long url to short url in a rails app like
snipurl.com or dwarfurl.com

Please help

THanks

Christophe Porteneuve

未读,
2007年5月30日 04:27:302007/5/30
收件人 rubyonrail...@googlegroups.com
Hey,

This is more of a general question or Rails question than Rails
Spinoffs. Next time, consider posting to RubyOnRails-Talk instead.

Gcompany a écrit :


> how do we convert long url to short url in a rails app like
> snipurl.com or dwarfurl.com

1) Devise a short ID generation algorithm (can be anything, really)

2) Store a mapping from the short, generated ID to the long URL you want
in a DB table somewhere.

3) Provide a controller that takes the short ID as a parameter, reads
the DB table and issues a redirect to the long URL.

Now, unless you're on a GET access with plenty of arguments inline,
Rails app URLs are usually darn short.

--
Christophe Porteneuve aka TDD
t...@tddsworld.com

Peter De Berdt

未读,
2007年5月30日 04:42:002007/5/30
收件人 rubyonrail...@googlegroups.com

On 30 May 2007, at 10:24, Gcompany wrote:

how do we convert long url to short url in a rails app like

snipurl.com or dwarfurl.com


You're asking on the wrong list, but here we go:

Create a model with these fields:
identifier
target_url

Each time a user enters a long url, add it to the table as the target url, and have a before_filter generate a unique hash-like identifier. Then have your routes include the identifier as the parameter. Just make sure your identifier is unique.

Basically you're just keeping a lookup table, very very easy.


Best regards


Peter De Berdt


回复全部
回复作者
转发
0 个新帖子