user241126
user241126

Reputation: 93

How can I find the final URL after all redirections in Perl?

Lets say I have "http://www.ritzcarlton.com" and that redirects me to "http://www.ritzcarlton.com/en/Default.htm". Is there a way in Perl to find the end url after all the redirects?

Upvotes: 6

Views: 2389

Answers (2)

mopoke
mopoke

Reputation: 10685

Using LWP will follow the redirections for you. You can then interrogate the HTTP::Request object to find out the URI it requested.

use LWP::UserAgent qw();

my $ua = LWP::UserAgent->new;

my $response = $ua->get('http://www.ritzcarlton.com');

print $response->request->uri . "\n";

Output is:

http://www.ritzcarlton.com/en/Default.htm

Upvotes: 18

friedo
friedo

Reputation: 66967

If you're issuing HTTP requests yourself, then the redirect URL will be in the returned Location: header. If you're using a proper HTTP client like LWP::UserAgent or WWW::Mechanize, which is what you should be doing, redirection is handled automatically.

Upvotes: 0

Related Questions