So we've redone our website. Now I want to make sure all the redirects are going to the correct place. What I want to do is request the old URL and see if the server gives a 301 code with the correct new url.

I found Is it possible to get the redirected URL?, but that didn't seem to have the answer. My script:

use LWP; my $url = "oldurl"; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req); print $res->status_line;

Now running the above code, it seems to realize the oldurl is redirected and pulls the new url and then gives me a 200 OK for the status line. I need to figure out how to get the 301 status from the url I gave it and not the new url it seems to pull from. Can someone help me?

Thanks in advance.

Brief Update: I know the url in question is being redirected b/c of this:

[xorl@xorlsbox ~/tools]$telnet ourdomainname 80 Trying xxx.xxx.xxx.xxx... Connected to ourdomainanme (xxx.xxx.xxx.xxx). Escape character is '^]'. GET /olddir HTTP/1.1 host: ourdomainname HTTP/1.1 301 Moved Permanently Date: Mon, 10 Aug 2009 17:18:45 GMT Server: Apache/2.0.52 (Red Hat) Accept-Ranges: bytes Set-Cookie: PHPSESSID=c100bc24fda84639acb995ae36a4f8c4; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre- +check=0 Pragma: no-cache Set-Cookie: PHPSESSID=17c937ef60bcd4d587ba9f662384292e; path=/ Set-Cookie: PHPSESSID=66db955db7d2e6987d235d8327b0abee; path=/ Location: /somecrazy/new/location/here/ Content-Length: 0 Content-Type: text/html; charset=utf-8 Connection closed by foreign host.
Update 2: Trying WWW::Mechanize now. Will post results. Update 3: WWW::Mechanize worked although the redirect_ok[0] solution did not. Here's the relevant code:
my $mech = WWW::Mechanize->new(); $mech->requests_redirectable([]); $mech->get($url); print $mech->response->code . " " . $mech->response->header("Locatio +n");

In reply to Get redirected URL by xorl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.