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

I need to redirect from a Perl script that is passed the value "shop" in a string E.g. http://mysite.co.uk/cgi/shop/redirect.pl?shop=rg

I need this to redirect to a section of a new shop with an address like: mynewsite.co.uk/store/index.php?cPath=1

I've been told it would look something like this:

#!/usr/bin/perl use CGI 'standard'; $shop = param('shop'); if ($shop eq 'rg') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=6'); }elsif ($shop eq 'ps') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=1'); }elsif ($shop eq 'sk') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=19'); }elsif ($shop eq 'smm') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=9'); }else{ print redirect('https://www.planetdistribution.co.uk/store'); }


But I can't get it to work as hard as I try. Is there any more coding I need to do to the page, I've been setting the permissions to 777. Is this right?

Help!

Replies are listed 'Best First'.
Re: Perl Redirect - Complete Perl Newbie
by gellyfish (Monsignor) on Feb 11, 2005 at 10:03 UTC

    When you say you "Can't get it to work" what exactly is happening? Are you getting an error, or are the redirects just not working. If you are in control of the server you should be able to get more detail from the error log of the web server.

    However it is almost certain that the actual problem is that you should have:

    use CGI ':standard';
    Rather than:
    use CGI 'standard';

    /J\

      Thanks for the advice and sorry for the lack of information. I'm getting a HTTP 500 error...?

      Adding the colon didn't seem to make any difference.

      This is the full page code:
      #!/usr/bin/perl use CGI ':standard'; $shop = param('shop'); if ($shop eq 'rg') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=6'); }elsif ($shop eq 'ps') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=1'); }elsif ($shop eq 'sk') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=19'); }elsif ($shop eq 'smm') { print redirect('https://www.planetdistribution.co.uk/store/index.php?c +Path=9'); }else{ print redirect('https://www.planetdistribution.co.uk/store'); }
Re: Perl Redirect - Complete Perl Newbie
by Fang (Pilgrim) on Feb 11, 2005 at 10:15 UTC

    If the code you're trying is the one you're showing us, taking a look at the error_log will probably tell you something along the line of Undefined subroutine redirect called at redirect.pl line 8 (and probably the same for the param() sub). To properly import the function interface from CGI.pm, you have to use CGI ':standard'; (or use CGI qw(:standard); as is often seen).

    Then, you should use strict;, use warnings;, as well as redirect errors and warnings to your browser while developping with use CGI::Carp qw(fatalsToBrowser warningsToBrowser);. When setting your variable from the CGI parameter 'shop', you'd better do it as my $shop = lc(param('shop')). This has the advantage of standardizing the case, so to speak, as well as making $shop equal to the empty string in case param('shop') is undefined, avoiding some warnings with the following tests.

      Thanks for the tips, some error messages on screen would be helpful. I'm still getting the http 500 error. Have I set it up incorrectly? There are other perl scripts running on the server?

        The following code works perfectly here.

        #!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $shop = lc(param('shop')); if ($shop eq "rg") { print redirect('https://www.planetdistribution.co.uk/store/index.p +hp?cPath=6'); } elsif ($shop eq "ps") { print redirect('https://www.planetdistribution.co.uk/store/index.p +hp?cPath=1'); } elsif ($shop eq "sk") { print redirect('https://www.planetdistribution.co.uk/store/index.p +hp?cPath=19'); } elsif ($shop eq "smm") { print redirect('https://www.planetdistribution.co.uk/store/index.p +hp?cPath=9'); } else { print redirect('https://www.planetdistribution.co.uk/store'); }

        HTTP Error 500 is an Internal Server Error if I'm not mistaken, so the problem lies in your web server configuration. As always, check the error_log.

Re: Perl Redirect - Complete Perl Newbie
by samizdat (Vicar) on Feb 11, 2005 at 13:31 UTC
    First, try REALLY HARD to get access to the server logs (either somewhere under /usr/local/apache or in /var logs. They're typically world-readable (if you have an account on the server, anyway!) and will tell you what's going on. They might be called anything, but you'll usually see 'http' and 'error' in the filename.

    Second, I would try seeing if the redirects work without making a transition to the https: server. In other words, see if you can get yourself to a pure HTML page. Do the https: pages come up if you manually type the URL in?
      All of the paths work, I've tested them. I managed to find my logs and make sure I was uploading with ASCII.

      Logs said this:
      Fri Feb 11 12:13:32 2005 error (2)No such file or directory: exec of /var/www/virtual/rhp/www/cgi/shop/cart.pl failed
      Fri Feb 11 12:13:32 2005 error client 83.166.176.8 Premature end of script headers: /var/www/virtual/rhp/www/cgi/shop/cart.pl

      cart.pl is the file I'm working on. Any idea why this is happening?
        In your Apache httpd.conf, is '.pl' defined as a cgi-executable extension?

        Also, how are you _calling_ this cgi? In CGI, you must reference a web-centric directory, which must then (in httpd.conf) be aliased to a real filesystem directory.