You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by andy <an...@andycrichton.co.uk> on 2012/10/15 18:23:48 UTC

[users@httpd] Re: RewriteCond - Multiple matches

Just to post my config and some more detail about the header matching 
problem.

RewriteEngine On
RewriteCond %{HTTP:MY-HEADER} (INTERESTING_[a-z0-9_]+).*+ [NC]
RewriteRule .* - [E=THING1:%1]
RewriteRule .* - [E=THING2:%2]
RequestHeader set OUTPUT-HEADER "%{THING1}e,%{THING2}e"

The reason behind the multiple RewriteRules was the fact that I 
couldn't find a way to put a comma into the environment setting part as 
it took that as the delimiter for the flags, escaping and quoting didn't 
appear to help.

So if I put this header in
INTERESTING_1,something,else,INTERESTING_2,INTRESTING_3

I only ever get the first match i.e. %1=INTERESTING_1.



If I do this

RewriteCond %{HTTP:MY-HEADER} .*(INTERESTING_[a-z0-9_]+) [NC]
then I get the last one


What I am hoping is that there is a nice way of matching multiple of 
them and reconstructing into a comma separated list, I can cope with 
having to list them out explicitly but am struggling to get a regex 
which allows me to do this.

I would normally in Perl have used the /g modifier however the only 
modifiers I have are NC,L and the slash is taken literally so /g tries 
to match "/g".


Any pointers will be greatly appreciated.

Cheers

Andy.


On 2012-10-15 12:16, andy wrote:
> I am having a problem which I believe is related to greedy matching.
>
> Header on the way in
>
> MY-HEADER: interesting_1,something_else,interesting_2,interesting_3
>
> and I want to set an environment variable then use mod_headers to
> replace it with
>
> MY-HEADER: interesting_1,interesting_2,interesting_3
>
> Whatever I try I only appear to get the first or last instance
> matched and was expecting to be able to build my output from %1,%2,%3
> in a RewriteRule right after the RewriteCond.
>
> Any help appreciated.
>
> Cheers
>
> Andy.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Re: RewriteCond - Multiple matches

Posted by Mark Montague <ma...@catseye.org>.
On October 15, 2012 12:23 , andy <an...@andycrichton.co.uk> wrote:
> RewriteEngine On
> RewriteCond %{HTTP:MY-HEADER} (INTERESTING_[a-z0-9_]+).*+ [NC]
> RewriteRule .* - [E=THING1:%1]
> RewriteRule .* - [E=THING2:%2]
> RequestHeader set OUTPUT-HEADER "%{THING1}e,%{THING2}e"
>
> [...] So if I put this header in
> INTERESTING_1,something,else,INTERESTING_2,INTRESTING_3
>
> I only ever get the first match i.e. %1=INTERESTING_1.

Correct.  %1 matches what is in the first set of parentheses for 
RewriteCond, %2 matches what is in the second set, and so on.  You only 
have one set of parentheses.

The following will set both %1 and %2, where %1 will begin with 
"INTERESTING_" and %2 may or may not begin with "INTERESTING_":

RewriteCond %{HTTP:MY-HEADER} (INTERESTING_[a-z0-9_]+),([a-z0-9_]+).*+ [NC]


You didn't say what version of Apache HTTP Server you're using, so let's 
say you're running httpd 2.4.3.  In that case, using "RequestHeader 
edit*" may be a more elegant way to do what you want -- something along 
the lines of:

RequestHeader edit* MY-HEADER (?=^|,)(?!INTERESTING_)[a-zA-Z0-9_]+(,|$) ""

I haven't tested this, and you may need to experiment quite a bit with 
the regular expression syntax to get things working.  What I'm 
attempting to say here is, edit to request header to remove all 
instances of things at the beginning of the header's value or following 
commas which do not begin with the string "INTERESTING_" but consist of 
a sequence of alphanumeric characters and underscores followed by either 
a comma or the end of the header.

https://httpd.apache.org/docs/2.4/mod/mod_headers.html#requestheader

Note that the edit* option to RequestHeader is not available in Apache 
HTTP Server 2.2.  See the PCRE documentation for what's legal in regular 
expressions.  Keep in mind that PCRE does not support perl delimiters 
(that is, enclosing the regular expression in slashes) nor does PCRE 
support Perl regular expression modifiers such as 'g', 'i', 'm', 's', or 
'x'.


Hopefully another member of this mailing list will be able to suggest a 
more elegant or better tested solution.

--
   Mark Montague
   mark@catseye.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org