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

Ok here's the problem. after installing mod_perl I placed the script I wanted to run into the perl directory called it and all worked swimmingly.

The script is called by lots of clients on different web servers So I cannot easily change the location of the script to /perl/

An easy fix or so I thought was to rewrite the url using mod rewrite so that the server would look for the script in the correct directory (/perl/) rather than the requested directory (/cgi-bin/)

I inserted this into httpd.conf in the virtual server configuration
<code>#redirect mlserver requests to the faster mod_perl
RewriteEngine on
RewriteRule ^/cgi-bin/mlserver.pl(.*) /perl/mlserver.pl$1</code>

Bonza it worked

But then I realised it was still running under CGI not mod_perl!

Now I cannot find a way to get the script running under mod_perl. CGI always takes over!
If I remove the shebang line the script fails
If I call it directly ie: server.com/perl/mlserver.pl it works correctly under mod_perl

How can I redirect an incoming request to the new mod_perl script?

I know this question is probably more an apache one, but I hope someone here can assist

I have tried renaming the script to mlserver.perl and the setting a new handler

ie: AddHandler perl-script .perl

but all that does is send back plain text, it doesn't execute mod_perl at all

Any ideas apart from telling all my clients to change the code on hundreds of sites?

  • Comment on Seems simple enough how do I redirect old cgi scripts to mod perl?

Replies are listed 'Best First'.
Re: Seems simple enough how do I redirect old cgi scripts to mod perl?
by skx (Parson) on Sep 30, 2005 at 12:50 UTC

    Change the rewrite rule to do an explicit redirect, with something like the following?

    #redirect mlserver requests to the faster mod_perl RewriteEngine on RewriteRule ^/cgi-bin/mlserver.pl(.*) /perl/mlserver.pl$1 [R]
    Steve
    --

      A word of warning -- it's a good idea when using R to use R,L, so it then doesn't try to do further processing on this request.

      Now for the problem -- R can cause havoc with some browsers if they're trying to POST (which is very likely for a CGI). It would be good to read section 10.3 in the HTTP specs, and some of the POSTredirect test results.

      Basic summary -- you would think you want 301 (moved permanently), but it screws up browsers; you'll want to use 303 for an HTTP/1.1 browser, and 302 for an HTTP/1.0 browser. (0.9 browsers don't support redirection in HTTP, as they don't pass any headers), and you'll want to test heavily.

      As another alternative, you can just configure the system to serve that particular URL as mod_perl, as opposed to CGI, without any need for redirection:

      <Location /cgi-bin/mlserver.pl> SetHandler perl-script PerlHandler Apache::Registry Options ExecCGI allow from all PerlSendHeader On </Location>
Re: Seems simple enough how do I redirect old cgi scripts to mod perl?
by rvosa (Curate) on Sep 30, 2005 at 13:20 UTC
    A CGI script doesn't automatically become a mod_perl "script". It is still executed faster, because the perl interpreter now is memory-resident inside apache, but to fully benefit from mod_perl you will have to rework your code into a handler. The mod_perl manual describes how this is done.
Re: Seems simple enough how do I redirect old cgi scripts to mod perl?
by cbrandtbuffalo (Deacon) on Sep 30, 2005 at 11:43 UTC
    When you say mod_perl, I assume you mean Apache::Registry. Can you post the directives you are using to activate Apache::Registry for that directory?
Re: Seems simple enough how do I redirect old cgi scripts to mod perl?
by perrin (Chancellor) on Sep 30, 2005 at 18:42 UTC
    You don't need to move the script. You can leave it in the cgi-bin directory and put in a <Location> block with the full URL of the individual script, and specify the SetHandler and Apache::Registry stuff in there.
      Wow thanks guys for all the input. Damn (oops not very monkish) I should have joined here years ago.

      I am just learning the ins and outs of mod_perl so I followed the instructions of how to set it up

      didn't even think of setting the individual script to a mod_perl script. just stuck to the paradigm of a directory


      I'll try this solution

      <Location /cgi-bin/mlserver.pl>
      SetHandler perl-script
      PerlHandler Apache::Registry
      Options ExecCGI
      allow from all
      PerlSendHeader On
      </Location>

      its great because it also cuts out the rewrite requirement

      Rewrite the script as an apache handler ... Great idea that would be much faster. I'll have to work out how to talk to mySQL from Apache but I am sure theres lots of info about that lying around.

      Steve I already ruled out a "redirect" because the script is called thousands of times per hour and the client times out if there is no response within 5 seconds.

      I can't afford the extra time and network activity a redirect introduces.

      Great Answers guys I'm impressed :) And your probably saying "Doh".
      I'll get there