# Retrieve redirect URLs from an URL

**URL:** <https://www.ruby-forum.com/t/retrieve-redirect-urls-from-an-url/186113>\
**Category:** Rails\
**Created:** [March 31, 2010, 6:19pm UTC](https://www.ruby-forum.com/t/retrieve-redirect-urls-from-an-url/186113 "2010-03-31T18:19:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [March 31, 2010, 6:19pm UTC](https://www.ruby-forum.com/t/retrieve-redirect-urls-from-an-url/186113/1 "2010-03-31T18:19:43Z")

</div>

For a project I am currently working on I am looking for a solution that  
gives me an ordened list of all the URLs that are in a redirect chain.

Lets say I have an URL x, clicking this in a normal browser would  
redirect to y, which would redirect me to z; my final destination.

Having just URL x, how can I get URL y and z? Do I need to use cURL to  
extract the heads recursively from all urls until there are no more  
redirects? How would I go about doing this?

Thanks.

---

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [April 6, 2010, 1:28am UTC](https://www.ruby-forum.com/t/retrieve-redirect-urls-from-an-url/186113/2 "2010-04-06T01:28:47Z")

</div>

Anyone got any advice on this situation?

---

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [April 6, 2010, 2:06am UTC](https://www.ruby-forum.com/t/retrieve-redirect-urls-from-an-url/186113/3 "2010-04-06T02:06:15Z")

</div>

Tom Pett wrote:

> Anyone got any advice on this situation?

* * *

dude,

do u require to perform any operation btw each redirect??

Reply…

---

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [April 8, 2010, 1:42am UTC](https://www.ruby-forum.com/t/retrieve-redirect-urls-from-an-url/186113/4 "2010-04-08T01:42:16Z")

</div>

I have found the gem HTTParty which is supposed to help me with this  
task.

I am having some troubles getting this to work on my environment.  
Currently I run Rails 2.3.5, HTTParty 0.5.2 on Windows Vista (developer  
machine).

I would really appreciate it if someone could give me an example code  
snippit on how I should do this.

Again, imagine the following: URL A redirect to URL B. B redirects to C.  
C is a normal website that can be retrieved via GET.

By supplying A, I want an array in return containing [A,B,C]

---

<div class="post-metadata">

**Author:** ![twp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@twp](https://www.ruby-forum.com/u/twp)\
**Post date:** [April 7, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/retrieve-redirect-urls-from-an-url/186113/5 "2010-04-07T17:00:33Z")

</div>

Rutvij P. wrote:

> Tom Pett wrote:
> 
> > Anyone got any advice on this situation?
> 
> * * *
> 
> dude,
> 
> do u require to perform any operation btw each redirect??
> 
> Reply…

Not really. I would just like a list of (chain) redirects to write in a  
database. I just want to extra all redirect information from an URL at  
any given time. Nothing has to be computed real time or during the  
redirects for my application.
