Bug 48351 - Ampersand taken to mean "what is matched by the previous regex" - unexpected given the "perl color" of regexes
Summary: Ampersand taken to mean "what is matched by the previous regex" - unexpected ...
Status: RESOLVED FIXED
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: Core (show other bugs)
Version: 2.0.54
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords: FixedInTrunk
Depends on:
Blocks:
 
Reported: 2009-12-08 12:08 UTC by Francis Galiegue
Modified: 2012-02-26 17:01 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Francis Galiegue 2009-12-08 12:08:36 UTC
If we take this sample:

RedirectMatch permanent ^/foo/?$ http://some.external.site/?var1=val1&var2=val2&var3=val3

and the URL entered is:

http://first.site/foo/

the redirect is to:

http://some.external.site/?var1=val1/foo/var2=val2/foo/var3=val3

Therefore, the & has extended to the whole text matched by the "left" regex, just like it does with "classic dialect" regex engines such as sed and vi.
The same behavior has been observed with SetEnvIf and RewrireRule. Probably other modules are affected as well.

This comes as quite a surprise given that Apache uses PCRE. I'd have expected $& to work, but not a plain ampersand.

This is true of Apache 2.0.54 and also 2.2.9, installed by default on Centos/RHEL 4.x and 5.x respectively. But this also holds true for the Apache 2.2.x coming with the latest Debian (I don't remember the exact version) and Apache 2.2.14 on Gentoo.

For coherency, I'd say that $& should replace & for this purpose, to be more inline with perl's regexes. But then it seems that Apache has behaved this way for quite a while (since 2.0.x times at least, I don't know for 1.3) and this change could break some setups...
Comment 1 Stefan Fritsch 2009-12-12 12:29:42 UTC
This is an undocmented special case in ap_pregsub: & is an alias for $0. mod_rewrite doesn't implement the same special behaviour.

I therefore agree that this is surprising and should be dropped. And introducing $& as alias for $0 is a good idea, too, because people may expect that from perl.
Making this change in 2.4 should be no problem as long as it is documented somewhere.
Comment 2 Stefan Fritsch 2010-01-30 03:21:25 UTC
Removed use of & in r904765
Comment 3 Stefan Fritsch 2012-02-26 17:01:04 UTC
fixed in 2.4.1