You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Les Mikesell <le...@home.com> on 2001/01/04 15:36:18 UTC

Rewrite arguments?

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



Re: [OT] Rewrite arguments?

Posted by Christopher Taranto <ch...@tokpela.com>.
Would something like RewriteMap work?

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

At 09:43 PM 1/5/01 -0600, Les Mikesell wrote:

>----- Original Message -----
>From: "G.W. Haywood" <ge...@www.jubileegroup.co.uk>
>To: "Les Mikesell" <le...@home.com>
>Cc: <mo...@apache.org>
>Sent: Friday, January 05, 2001 1:44 PM
>Subject: Re: [OT] Rewrite arguments?
>
>
> > 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.
>
>        Les Mikesell
>          lesmikesell@home.com


Re: [OT] Rewrite arguments?

Posted by Les Mikesell <le...@home.com>.
----- Original Message -----
From: "Dave Kaufman" <dk...@nac.net>
To: "Les Mikesell" <le...@home.com>
Cc: <mo...@apache.org>
Sent: Friday, January 05, 2001 11:09 PM
Subject: Re: [OT] Rewrite arguments?


> > 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)

    Les Mikesell
       lesmikesell@home.com



Re: [OT] Rewrite arguments?

Posted by Dave Kaufman <dk...@nac.net>.
"Les Mikesell" <le...@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,

-dave




Re: [OT] Rewrite arguments?

Posted by Les Mikesell <le...@home.com>.
----- Original Message -----
From: "G.W. Haywood" <ge...@www.jubileegroup.co.uk>
To: "Les Mikesell" <le...@home.com>
Cc: <mo...@apache.org>
Sent: Friday, January 05, 2001 1:44 PM
Subject: Re: [OT] Rewrite arguments?


> 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.

       Les Mikesell
         lesmikesell@home.com


Re: [OT] Rewrite arguments?

Posted by "G.W. Haywood" <ge...@www.jubileegroup.co.uk>.
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.
----------------------------------------------------------------------

73,
Ged.