modperl_rewrite

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: <modperl@apache.org>
From: "Les Mikesell" <lesmikesell@home.com>
Subject: Rewrite arguments?
Date: Thu, 4 Jan 2001 08:36:18 -0600

This may or may not be a mod_perl question: 
I want to change the way an existing request is handled and it can be done
by making a proxy request to a different host but the argument list must
be slightly different.    It is something that a regexp substitution can
handle and I'd prefer for the front-end server to do it via mod_rewrite
but I can't see any way to change the existing arguments via RewriteRules.
To make the new server accept the old request I'll have to modify the name
of one of the arguments and add some extra ones.  I see how to make
mod_rewrite add something, but not modify the existing part. Will I
have to let mod_perl proxy with LWP instead or have I missed something
about mod_rewrite?   (Modifying the location portion is easy, but the
argument list seems to be handled separately).

    Les Mikesell
      lesmikesell@home.com

===

To: Les Mikesell <lesmikesell@home.com>
From: "G.W. Haywood" <ged@www.jubileegroup.co.uk>
Subject: Re: [OT] Rewrite arguments?
Date: Fri, 5 Jan 2001 19:44:04 +0000 (GMT)

Hi there,

Didn't see a reply to this yet...

On Thu, 4 Jan 2001, Les Mikesell wrote:

> This may or may not be a mod_perl question: 

Probably not :)

> I want to change the way an existing request is handled and it can be done
> by making a proxy request to a different host but the argument list must
> be slightly different.    It is something that a regexp substitution can
> handle and I'd prefer for the front-end server to do it via mod_rewrite
> but I can't see any way to change the existing arguments via RewriteRules.

I don't exactly understand your problem, but from what I can see you
should be able to do what you want with mod_rewrite if you just use a
regexp which contains a question mark.  Have I missed something?

Does this extract from the docs help?
  ----------------------------------------------------------------------
One more note: You can even create URLs in the substitution string containing
a query string part. Just use a question mark inside the substitution string
to indicate that the following stuff should be re-injected into the
QUERY_STRING.  When you want to erase an existing query string, end the
substitution string with just the question mark.

Note: There is a special feature: When you prefix a substitution field
with http://thishost[:thisport] then mod_rewrite automatically strips
it out.  This auto-reduction on implicit external redirect URLs is a
useful and important feature when used in combination with a
mapping-function which generates the hostname part.  Have a look at
the first example in the example section below to understand this.
 ----------------------------------------------------------------------
===

From: "Les Mikesell" <lesmikesell@home.com>
Subject: Re: [OT] Rewrite arguments?
Date: Fri, 5 Jan 2001 21:43:27 -0600

"G.W. Haywood" <ged@www.jubileegroup.co.uk> wrote:

> On Thu, 4 Jan 2001, Les Mikesell wrote:
>
> > This may or may not be a mod_perl question:
>
> Probably not :)

I have a feeling it is going to end up being possible only
with LWP...

> > I want to change the way an existing request is handled and it can be done
> > by making a proxy request to a different host but the argument list must
> > be slightly different.    It is something that a regexp substitution can
> > handle and I'd prefer for the front-end server to do it via mod_rewrite
> > but I can't see any way to change the existing arguments via RewriteRules.
>
> I don't exactly understand your problem, but from what I can see you
> should be able to do what you want with mod_rewrite if you just use a
> regexp which contains a question mark.  Have I missed something?

One of us is missing something.  I hope it is me, but when I turn on
rewrite logging, the input side contains only the location portion.  The
argument string has already been stripped.  Apparently it is put back
in place after the substition, since  ^(.*)$  http://otherserver$1  [P] will
send the same arguments on to the downstream host.

> Does this extract from the docs help?
> ----------------------------------------------------------------------
> One more note: You can even create URLs in the substitution string containing
> a query string part. Just use a question mark inside the substitution string
> to indicate that the following stuff should be re-injected into the
> QUERY_STRING.  When you want to erase an existing query string, end the
> substitution string with just the question mark.

This allows adding additional arguments, or deleting them all.  I want to
change an existing one and add some more.  Something like:
/cgi-bin/prog?arg1=22&arg2=24 should become:
   http://otherhost.domain/prog?newarg1=22&arg2=24&uname=me&pwd=password


> Note: There is a special feature: When you prefix a substitution field
> with http://thishost[:thisport] then mod_rewrite automatically strips
> it out.  This auto-reduction on implicit external redirect URLs is a
> useful and important feature when used in combination with a
> mapping-function which generates the hostname part.  Have a look at
> the first example in the example section below to understand this.

That won't affect this case.  The hostname will be fixed and always
require the proxy mode.

===


To: "Les Mikesell" <lesmikesell@home.com>
From: "Dave Kaufman" <dkaufman@nac.net>
Subject: Re: [OT] Rewrite arguments?
Date: Sat, 6 Jan 2001 00:09:48 -0500

"Les Mikesell" <lesmikesell@home.com> wrote:
>
> I have a feeling it is going to end up being possible only
> with LWP...
>
> > I don't exactly understand your problem, but from what I can see you
> > should be able to do what you want with mod_rewrite if you just use a
> > regexp which contains a question mark.  Have I missed something?
>
> One of us is missing something.  I hope it is me, but when I turn on
> rewrite logging, the input side contains only the location portion.  The
> argument string has already been stripped.

the query string is stripped from what the rewrite rule is matching, yes.  but
you can use a RewriteCond above the rule to test %{QUERY_STRING} against a
regexp pattern, and store backreferences from it as %1, %2...etc.

> > Does this extract from the docs help?
> > ----------------------------------------------------------------------
> > One more note: You can even create URLs in the substitution string
containing
> > a query string part. Just use a question mark inside the substitution
string
> > to indicate that the following stuff should be re-injected into the
> > QUERY_STRING.  When you want to erase an existing query string, end the
> > substitution string with just the question mark.

> This allows adding additional arguments, or deleting them all.  I want to
> change an existing one and add some more.  Something like:
> /cgi-bin/prog?arg1=22&arg2=24 should become:
>    http://otherhost.domain/prog?newarg1=22&arg2=24&uname=me&pwd=password

Also from the RTFM dept:

'qsappend|QSA' (query string append)
This flag forces the rewriting engine to append a query string part in the
substitution string to the existing one instead of replacing it. Use this when
you want to add more data to the query string via a rewrite rule.

a possibly relevant example would be:

# match and store the interesting arg values as backrefs
RewriteCond %{QUERY_STRING} arg1=([0-9]+)&arg2=([0-9]+)
# build a new QS for the proxy url
RewriteRule ^/cgi-bin/prog
http://otherhost/prog?newarg1=%1&arg2=%2&uname=me&pwd=password [R,L]

(but without the linewrap, of course)

in this example you dont need the qsappend flag because we reconstructed the
entire query string.  if you werent renaming agr1 to newarg1 (or if ther may
have been other args you want to pass on) you could have just done:

# just test for the presence of our args in the QS
# and make the whole thing conditional on that
RewriteCond %{QUERY_STRING} arg1=[0-9]+&arg2=[0-9]+

#and rewite, injecting just the new args
RewriteRule ^/cgi-bin/prog
http://otherhost/prog?uname=me&pwd=password [QSA,R,L]

and the rw engine will add uname & pwd to any existing querystring that was
present

hope this helps,

===

To: Les Mikesell <lesmikesell@home.com>
From: Christopher Taranto <christopher@tokpela.com>
Subject: Re: [OT] Rewrite arguments?
Date: Fri, 05 Jan 2001 21:55:58 -0800

Would something like RewriteMap work?

http://httpd.apache.org/docs/mod/mod_rewrite.html#RewriteMap

===

From: "Les Mikesell" <lesmikesell@home.com>
Subject: Re: [OT] Rewrite arguments?
Date: Sat, 6 Jan 2001 00:15:38 -0600

"Dave Kaufman" <dkaufman@nac.net> wrote:

> > One of us is missing something.  I hope it is me, but when I turn on
> > rewrite logging, the input side contains only the location portion.  The
> > argument string has already been stripped.
>
> the query string is stripped from what the rewrite rule is matching, yes.
but
> you can use a RewriteCond above the rule to test %{QUERY_STRING} against a
> regexp pattern, and store backreferences from it as %1, %2...etc.

That's it - thank you very much.  I had seen how to match and reuse chunks
in the RewriteCond, but somehow missed the ability to substitute them
in the RewriteRule.  I should have known it was too useful to have
been left out.

> # match and store the interesting arg values as backrefs
> RewriteCond %{QUERY_STRING} arg1=([0-9]+)&arg2=([0-9]+)
> # build a new QS for the proxy url
> RewriteRule ^/cgi-bin/prog
> http://otherhost/prog?newarg1=%1&arg2=%2&uname=me&pwd=password [R,L]

Since I only want to substitute one argument name without knowing much
else I think this will work:
RewriteCond      %{QUERY_STRING} (.*)(arg1=)(.*)
RewriteRule ^/cgi-bin/prog
http://otherhost/prog?%1newarg1=%2&uname=me&pwd=password [P,L]
(I want a proxy request to hide the password usage, not a client redirect
but either could work)

===

To: modperl@apache.org
From: Tomas Edwardsson <tommi@mekkano.is>
Subject: Using rewrite...
Date: Fri, 19 Jan 2001 10:56:36 +0000

Hi

I'm using rewrite to send a request to a relevant server, for
instance if a filename ends with .pl I rewrite it to the perl
enabled apache:

RewriteEngine On

# Perl Enabled.
RewriteRule ^/(.*\.ehtm)$ http://%{HTTP_HOST}:81/$1 [P]
RewriteRule ^/(.*\.pl)$ http://%{HTTP_HOST}:81/$1 [P]
# PHP Enabled
RewriteRule ^(.*\.php)$ http://%{HTTP_HOST}:83$1 [P]
# Everyting else, images etc...
RewriteRule ^/(.*)$ http://%{HTTP_HOST}:82/$1 [P]

The problem is that I can't find a way to send the request
to a relevant port if the request calls for a URL which ends
with a slash ("/"). Any hints ?

===

To: Tomas Edwardsson <tommi@mekkano.is>
From: Matthew Byng-Maddick <mbm@colondot.net>
Subject: Re: Using rewrite...
Date: Fri, 19 Jan 2001 10:59:43 +0000 (GMT)

On Fri, 19 Jan 2001, Tomas Edwardsson wrote:
> The problem is that I can't find a way to send the request
> to a relevant port if the request calls for a URL which ends
> with a slash ("/"). Any hints ?

RewriteCond and %{REQUEST_FILENAME} ?

This happens after the default URI Translation handler.

===

To: Matthew Byng-Maddick <mbm@colondot.net>
From: Tomas Edwardsson <tommi@mekkano.is>
Subject: Re: Using rewrite...
Date: Fri, 19 Jan 2001 11:13:27 +0000

RewriteCond %{REQUEST_FILENAME} .*\.php$
RewriteRule ^(.*)$ http://%{HTTP_HOST}:83$1

I Tested it like this and this doesn't seem to work, either
I'm misunderstanding RewriteCond or this method doesn't work.

===

To: Tomas Edwardsson <tommi@mekkano.is>
From: Matthew Byng-Maddick <mbm@colondot.net>
Subject: Re: Using rewrite...
Date: Fri, 19 Jan 2001 11:32:34 +0000 (GMT)

On Fri, 19 Jan 2001, Tomas Edwardsson wrote:
> RewriteCond %{REQUEST_FILENAME} .*\.php$
> RewriteRule ^(.*)$ http://%{HTTP_HOST}:83$1
> I Tested it like this and this doesn't seem to work, either
> I'm misunderstanding RewriteCond or this method doesn't work.

What happens if you turn RewriteLog On and set RewriteLogLevel 9?

===

To: Matthew Byng-Maddick <mbm@colondot.net>
From: Tomas Edwardsson <tommi@mekkano.is>
Subject: Re: Using rewrite...
Date: Fri, 19 Jan 2001 11:40:27 +0000

It doesn't seem to apply the values of the DirectoryIndex to the filenames.

DirectoryIndex index.php index.ehtm index.pl index.html

RewriteCond %{REQUEST_FILENAME} .*\.php$
RewriteRule ^(.*)$ http://%{HTTP_HOST}:83$1

rewrite.log:
194.144.154.45 - - [19/Jan/2001:11:40:23 +0000] [dave.mekkano.com/sid#816ad70][rid#81b8f88/initial] (4) RewriteCond: input='/' pattern='.*\.php$' => not-matched

RewriteCond %{SCRIPT_FILENAME} .*\.php$
RewriteRule ^(.*)$ http://%{HTTP_HOST}:83$1

rewrite.log:
194.144.154.45 - - [19/Jan/2001:11:40:28 +0000] [dave.mekkano.com/sid#816ad70][rid#81b8f88/initial] (4) RewriteCond: input='/' pattern='.*\.php$' => not-matched

===

To: Tomas Edwardsson <tommi@mekkano.is>
From: Matthew Byng-Maddick <mbm@colondot.net>
Subject: Re: Using rewrite...
Date: Fri, 19 Jan 2001 11:43:04 +0000 (GMT)

On Fri, 19 Jan 2001, Matthew Byng-Maddick wrote:
> On Fri, 19 Jan 2001, Tomas Edwardsson wrote:
> > The problem is that I can't find a way to send the request
> > to a relevant port if the request calls for a URL which ends
> > with a slash ("/"). Any hints ?
> RewriteCond and %{REQUEST_FILENAME} ?
> This happens after the default URI Translation handler.

Oops. This appears that I'm lying. It doesn't work in my setup either. 

MBM (needs to go read some source.... :)

===
To: "Tomas Edwardsson" <tommi@mekkano.is>,
<modperl@apache.org>
From: "Les Mikesell" <lesmikesell@home.com>
Subject: Re: Using rewrite...
Date: Fri, 19 Jan 2001 22:52:22 -0600

"Tomas Edwardsson" <tommi@mekkano.is> wrote:
> 
> I'm using rewrite to send a request to a relevant server, for
> instance if a filename ends with .pl I rewrite it to the perl
> enabled apache:
> 
> RewriteEngine On
> 
> # Perl Enabled.
> RewriteRule ^/(.*\.ehtm)$ http://%{HTTP_HOST}:81/$1 [P]
> RewriteRule ^/(.*\.pl)$ http://%{HTTP_HOST}:81/$1 [P]
> # PHP Enabled
> RewriteRule ^(.*\.php)$ http://%{HTTP_HOST}:83$1 [P]
> # Everyting else, images etc...
> RewriteRule ^/(.*)$ http://%{HTTP_HOST}:82/$1 [P]
> 
> The problem is that I can't find a way to send the request
> to a relevant port if the request calls for a URL which ends
> with a slash ("/"). Any hints ?

Won't it work if you make the regexp match the slash too?  Something
like:
RewriteRule ^/(.*\.pl/*)$ http://%{HTTP_HOST}:81/$1 [P]
  or just duplicate the line with and without the slash in the pattern.

===



the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu