Forum Moderators: coopster & phranque

Message Too Old, No Replies

Apache mod_rewrite external map misery

.

         

kobi_inext

2:16 pm on May 7, 2003 (gmt 0)

10+ Year Member



Hello,

I set up a rewriting rule in apache 1.3.27 within a virtualhost directive, see below:

RewriteEngine on
RewriteLog /tmp/rewrite.log
RewriteLogLevel 9
RewriteMap shop prg:/tmp/map.pl
RewriteRule /*/$ ${shop:$1} [PT]

which is supposed to pass each urls to a perl script. Right now this script is very simple, just rewrites the given url to an external url, always the same (I'm just testing now).

#!/usr/bin/perl
$¦=1;

print "http://site.com";

Thats all. Now, I restart apache, and the first time I enter an URL which will match the rewriting rule, the perl script is run, and I get redirected to the another url with 302. This is fine so far. But any further tries (with the same URL,even) gives me 400 bad request errors. I restart apache, and it works fine, for the first redirection, then again, chokes on 400.

part of log file (first, succesfull redirection):

192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (2) init rewrite engine with requested uri /foo/
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (3) applying pattern '/*/$' to uri '/foo/'
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (5) map lookup OK: map=shop key= -> val=http://site.com/
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (2) rewrite /foo/ -> [site.com...]
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (2) implicitly forcing redirect (rc=302) with [site.com...]
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (2) forcing 'http://site.com/' to get passed through to next API URI-to-filename handler

Second and subsequent tries:

192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (2) init rewrite engine with requested uri /foo/
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (3) applying pattern '/*/$' to uri '/foo/'
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (5) map lookup OK: map=shop key= -> val=
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (2) rewrite /foo/ ->
192.168.0.139 - - [07/May/2003:15:56:26 +0200] [site.com/sid#81b0a9c][rid#81ec0d4/initial] (2) forcing '' to get passed through to next API URI-to-filename handler

So it seems the perl script didn't run the second time.
Anyone can enlighten me what I'm doing wrong?

[edited by: jatar_k at 3:23 pm (utc) on May 7, 2003]

Sorry forgot it.

daisho

7:53 pm on May 7, 2003 (gmt 0)

10+ Year Member



Your problem is the way you have written your perl script. Apache does not execute your program every time. It runs it all time time and communicates with it via standard input and you talk back via standard output.

This example will get you going:

#!/usr/bin/perl -i
$¦=1;
while (<STDIN>) {
# $_ -- This holds the requested url to rewrite

# This is what apache will acctually call.
print "http://mysite.com";
}

daisho.

kobi_inext

2:06 pm on May 8, 2003 (gmt 0)

10+ Year Member



Thanks Daisho!

That'll make it. I recall that I read it somewhere but forgot it.

Kobi

daisho

4:43 pm on May 8, 2003 (gmt 0)

10+ Year Member



Glad to help and Welcome to Webmaster World.

daisho.