How to convert a long url to short url

4 views
Skip to first unread message

Gcompany

unread,
May 30, 2007, 4:24:09 AM5/30/07
to 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

unread,
May 30, 2007, 4:27:30 AM5/30/07
to 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

unread,
May 30, 2007, 4:42:00 AM5/30/07
to 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


Reply all
Reply to author
Forward
0 new messages