mod_rewrite with prg rewritemap

New to mod_rewrite? This is a good place to start.

mod_rewrite with prg rewritemap

Postby alinux » Mon Jun 29, 2009 12:01 am

Hi
I got a mod_rewrite condition that relies on the output of a perl script, the perl script runs fine from command line, however when I try apache the condition is not matched.
Any help ?
Thanks


########.htaccess file

rewriteengine on
RewriteCond ${dash2score:%{REMOTE_ADDR}} yes
RewriteRule !^index.php$ http://www.howstuffworks.com [P,L]


#########apache2.conf
RewriteMap dash2score prg:/var/www/getIpStat.pl


#########getIpStat.pl
#!/usr/bin/perl
use DBI;
# Connect
my $dbh = DBI->connect('DBI:mysql:announcments:localhost','root', 'xxxxxxxx')
or die "Could not connect: $DBI::errstr\n";
# Get post_name
my $post_name = <STDIN>;
# my $post_name = $ARGV[$0];
chomp($post_name);
# Get data from db
my ($ID) = $dbh->selectrow_array("SELECT state FROM ips WHERE ip = ?", undef, $post_name);
# Display Results
print $ID;
alinux
 
Posts: 8
Joined: Tue Jun 16, 2009 5:15 am

Postby richardk » Mon Jun 29, 2009 11:18 am

Is "yes" returned by the script?
What is returned when there is no match?

Edit:

You only needed one thread.
Hi

.htaccess I got
Code: Select all
RewriteEngine on
RewriteCond ${dash2score:%{REMOTE_ADDR}} =yes
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^(.*)$ http://www.howstuffworks.com/$1 [P,L]


Script is :
Code: Select all
#!/usr/bin/perl

while (my $post_name = <STDIN>) {
print "yes";
}


Log is :
Code: Select all
192.168.0.189 - - [29/Jun/2009:13:03:54 +0300] [192.168.0.1/sid#9a48d18][rid#b61b5500/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/asdfas.php -> asdfas.php
192.168.0.189 - - [29/Jun/2009:13:03:54 +0300] [192.168.0.1/sid#9a48d18][rid#b61b5500/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'asdfas.php'
192.168.0.189 - - [29/Jun/2009:13:03:54 +0300] [192.168.0.1/sid#9a48d18][rid#b61b5500/initial] (5) map lookup FAILED: map=dash2score key=192.168.0.189
192.168.0.189 - - [29/Jun/2009:13:03:54 +0300] [192.168.0.1/sid#9a48d18][rid#b61b5500/initial] (4) [perdir /var/www/] RewriteCond: input='' pattern='=yes' => not-matched
192.168.0.189 - - [29/Jun/2009:13:03:54 +0300] [192.168.0.1/sid#9a48d18][rid#b61b5500/initial] (1) [perdir /var/www/] pass through /var/www/asdfas.php


Well since the script does echo yes on any input it should match right ?


(4) [perdir /var/www/] RewriteCond: input='' pattern='=yes' => not-matched

Nothing is returned by your script.

The documentation says
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap wrote:# A common mistake is to use buffered I/O on stdout. Avoid this, as it will cause a deadloop! ``$|=1'' is used above, to prevent this.

It also has an example script.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alinux » Tue Jun 30, 2009 5:25 am

Hi Thanks for the hint, for testing / debugging purposes I change the script to:

#!/usr/bin/perl
$| = 1;
while (<STDIN>) {
# ...put here any transformations or lookups...
print $_;
}

In other words i simply returns whatever you throw at it.

And I change .htaccess to:

RewriteEngine on
RewriteCond ${dash2score:yes} yes
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^(.*)$ http://www.howstuffworks.com/$1 [P,L]

So now, the dash2score perl script gets yes as an argument and it returns yes, i tested it from cmd line.

However, once I test it in apache all I get is:

192.168.0.189 - - [30/Jun/2009:16:19:26 +0300] [192.168.0.1/sid#89eaba8][rid#8be28e0/initial] (5) map lookup FAILED: map=dash2score key=yes
192.168.0.189 - - [30/Jun/2009:16:19:26 +0300] [192.168.0.1/sid#89eaba8][rid#8be28e0/initial] (4) [perdir /var/www/] RewriteCond: input='' pattern='yes' => not-matched


Thanks
alinux
 
Posts: 8
Joined: Tue Jun 16, 2009 5:15 am

Postby richardk » Tue Jun 30, 2009 4:30 pm

When you run it from the command line, does it wait for you to enter something, then output what you entered and then wait for more input?

Try adding a newline to the end of the print statement.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alinux » Tue Jun 30, 2009 10:27 pm

Hi
WHen run from cmd line it keeps waiting for input it does not terminate I did try with a new line in the script

print "\n";

It does show in cmd line that a new line is added..but from within apache the same problem exists.

THanks
alinux
 
Posts: 8
Joined: Tue Jun 16, 2009 5:15 am

Postby richardk » Wed Jul 01, 2009 8:49 am

Then i don't know what's wrong. You could try a RewriteMap written in a different language (to see if it works).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 15 guests

cron