2

I am trying to understand what this rewrite condition exactly does:

RewriteCond foo#%{ENV:bar} ^([^#]+)#\1$

As stated in "Using custom environment variables in .htaccess" this could be used as a workaround for testing environment variables in a rewrite condition - e.g. that "foo" equals the value of bar.

EDIT:

Thank you @miah for pointing out what the regex does. Although I still don't get why the variable can be tested in such a way, where it could not be tested directly.

UPDATE:

So when CondPattern does not evaluate the variable and it is evaluated in the TestString, why this does not work in comparison as bar is empty here:

RewriteCond %{ENV:bar} ^foo$
Community
  • 1
  • 1
conceptdeluxe
  • 3,753
  • 3
  • 25
  • 29

1 Answers1

1

The clever bit here is that \1 lets you reference a capture group from earlier in your regex.

  1. ^ - from the beginning of the string you are checking (in this case foo#%{ENV:bar})
  2. ( - start capture group
  3. [^#]+ - 1 or more characters that are not a #
  4. ) - end capture group
  5. # - the character #
  6. \1 - the capture group from line 2-3
  7. $ - the end of the string.

Update

The issue is that the CondPattern part of RewriteCond does not evaluate environment variables, and it must be a perl compatible regular expression, or a string.

miah
  • 10,093
  • 3
  • 21
  • 32
  • Thank you for exactly pointing out what the regex does, which is what I assumed it does ... but the part I don't get is, why the value of the variable is tested here although it could not be tested directly (as it would be empty). – conceptdeluxe Aug 21 '13 at 15:33