Rewrite map (prg:) never finishes

apache-2.2mod-rewrite

using Apache and a prg type rewrite map. My map looks like:

#!/usr/bin/perl
$| = 1; # Turn off buffering 
while (<STDIN>) {
    print "someothersite.com";
}

the rewrite rule declared in httpd.conf is:

RewriteMap app_map prg:/file/path/test.pl
RewriteRule  (\/[\w]+)(\/[^\#\s]+)?$ http://${app_map:$1}$2 [P,L]

And the log files show:

init rewrite engine with requested uri /a/testlink.html
applying pattern '(\/[\w]+)(\/[^\#\s]+)?$' to uri '/a/testlink.html'

It appears like test.pl is never giving control back to apache, when the map is successfully found I expect to see this output in the log file:

map lookup OK: map=app_map key=/a -> val=someothersite.com

Why is my map not returning control back to apache?

Best Answer

I got it working with the following:

#!/usr/bin/perl
#
##   disable buffered I/O which would lead
##   to deadloops for the Apache server
$| = 1;
#
##   read URLs one per line from stdin
while (<>) {
    my $line = $_;
    if ($line eq "input_from_apache\n"){
        print "my_desired_output\n";    
    }   
    else{
        print "\n";
    }
}

As best I can tell, the newline characters are what I was missing. For anyone trying to debug a RewriteMap script I suggest:

  1. Make sure you have:

    RewriteEngine On

    RewriteLog /var/log/httpd/rewrite.log

    RewriteLogLevel 9

    in your httpd.conf so you can see what mod_rewrite is doing

  2. Write your script, and then start it (i.e. ./my_script.pl) and type in some inputs to make sure you're getting what you expect. That's how I realized that I needed the \n's

Related Topic