Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Help needed on HTTP redirects

by pheonix (Novice)
on Aug 04, 2004 at 15:50 UTC ( #380026=perlquestion: print w/replies, xml ) Need Help??

pheonix has asked for the wisdom of the Perl Monks concerning the following question:

I was asked to write a piece of perl code to redirect current webpage A to webpage B in case that B is working. otherwise I have to redirect A to some different "friendly error pages" based on the error type received. I think I have to use the LWP, HTTP modules, but I have not found any "detailed" documentation about those modules, can any one do me a favor? at the same time, I am still not sure how to differentiate errors received? Can anyone give me some hints? :)

20040810 Edit by ysth: change title from: Can anyone help?

Replies are listed 'Best First'.
Re: Help needed on HTTP redirects
by friedo (Prior) on Aug 04, 2004 at 15:56 UTC
    You don't need to use LWP or HTTP modules to send a browser redirect; that's handled by the ever-present CGI module.

    #!/usr/bin/perl -T use strict; use warnings; use CGI; my $q = CGI->new; # send a redirect print $q->header(-location => 'http://www.foo.com/bar.html');

    If you need to check the status of the other pages, you may want to use LWP::Simple to fetch them and examine the contents.

      How about this deviation on the same theme:
      use CGI ':standard'; print CGI->redirect(-location=>"http://www.food.com/bar.html");
      Better yet, why not do some configuring in Apache. You could redirect the user to a diffent web page depending on the error message. Here are examples for error 404 and error 500, which are typical ones, that you could add to /etc/httpd/conf/httpd.conf configuration file or even to a .htaccess file.
      ErrorDocument 404 /error_msgs/error_notfound.html ErrorDocument 500 /error_msgs/error_scriptproblem.html
      See this article on Apache Redirection from Unix Review.

      -Spenser

      That's Spenser, with an "s" like the detective.

Re: Help needed on HTTP redirects
by Mr_Jon (Monk) on Aug 04, 2004 at 17:31 UTC
    To differentiate HTTP responses with a bit more granularity, use $response->code or $response->status_line:
    #! usr/bin/perl -w use strict; use LWP::UserAgent; $\ = "\n"; my $ua = LWP::UserAgent->new; my $response = $ua->get('http://www.perlmonks.org'); print $response->code; print $response->status_line; # or more generally... print "Success" if $response->is_success; print "Error" if $response->is_error; print "Info" if $response->is_info; print "Redirect" if $response->is_redirect;
    See the docs on LWP::UserAgent and HTTP::Response for more detail.
Re: Help needed on HTTP redirects
by johnnywang (Priest) on Aug 04, 2004 at 17:15 UTC
    You can use LWP::Simple::getprint("your page B") to get the return code, actually to check whether it's successful, simply do:
    is_success(getprint("http://your_page_B"))
    You can then send a redirect.
Re: Help needed on HTTP redirects
by danielcid (Scribe) on Aug 04, 2004 at 17:19 UTC
    You can also just use HTML to redirect:
    <META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://www.ossec.net">


    -DBC

      That is an abuse of HTML though. It should never have been adopted in the first place.

      Search engines and HTTP clients which don't parse HTML are likely not to honour this tag.

      Headers should be in the header, not in the body of a response.

      Makeshifts last the longest.

Re: detecting web server errors
by markjugg (Curate) on Aug 04, 2004 at 19:50 UTC
    Have you looked to using the web server to provide your solution? At least Apache already knows about several different cases including "moved", "missing", "internal server error" and "service unavailable. Web pages for any of these cases can be customized, and can even include dynamic information.

    I know Apache will allow you to provide custom error pages with some granularity, so one area of the website could have different error pages, if that's what you needed.

    This just seems simpler and more straightforward to me than using Perl to reproduce what is already an easy to configure feature.

Re: Help needed on HTTP redirects
by artist (Parson) on Aug 04, 2004 at 18:02 UTC
    I have comments on the title:

    It should be 'Redirecting Webpage' or something like that instead of 'Can anyone Help'. Perlmonks is a community based on concept of share and help.

      Sorry for my "inappropriate" words used, :) I am a freshman and I'd like to share what i know about Perl with all of the Perl people here in the future.
Re: Help needed on HTTP redirects
by Dog and Pony (Priest) on Aug 05, 2004 at 09:58 UTC
    Untested, but something like this should be well enough:
    #!/usr/bin/perl use warnings; use strict; use CGI 'redirect'; use LWP::Simple 'head'; if(head('http://siteB.com')) { print redirect('http://siteB.com'); } else { print redirect('/404'); # already at siteA }
    head should return true (and some info) only if the site asked is up and working. If it isn't enough, you might want to look at LWP head replacement, especially merlyn's answer.

    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Help needed on HTTP redirects
by mpeg4codec (Pilgrim) on Aug 05, 2004 at 02:28 UTC
    I'm not sure if you're looking for a strictly CGI.pm way of doing this, but to be overly simple:
    print "Location: http://www.example.com/foo.html\n\n";
    That would also require not using Content-type in the header.
Meditation (Re: Help needed on HTTP redirects)
by Anonymous Monk on Aug 06, 2004 at 18:19 UTC
    Because the first poster missed the point of the question(not how to redirect, but how to detect the status of the server), all other seemed to miss this point in one way or the other. The few(one?) who caught it seem to miss the other point(not redirect yes or no, but redirect in response to the status of the *other* server) and follow the herd who suggested ErrorDocuments. My Question is now:
    Does the tone of the already posted responses influence the reading of the question? And if many monks err, does one want to correct them or let them correct oneself?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://380026]
Approved by ysth
Front-paged by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2021-11-28 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?