I'm trying to exploit the power of Apache 2.4's <If>
directives to customize a <Macro>
by evaluating its parameters.
I'm currently trying to do the following:
- If authenticationMode == "htpasswd" use
.htpasswd
file - If authenticationMode == "server" use
pwauth
- Else break Apache configuration
The following code (inside of a <Macro>
tag) does not work
SetEnv ROOT_PATH /somewhere #Set outside macro
<If "$authenticationMode == 'htpasswd'"> # Guilty line!
AuthUserFile $ROOT_PATH/path/to/.htpasswd
</If>
<ElseIf "$authenticationMode == 'server'">
AuthBasicProvider external
AuthExternal pwauth
</ElseIf>
<Else>
DO_CRASH
</Else>
Error message is Cannot parse condition clause: syntax error, unexpected T_OP_STR_EQ, expecting '('
How to fix?
You can use
<If>
inside a<Macro>
, but not to customize a<Macro>
with<If>
.As mentioned in my comment, the
<Macro>
is evaluated only at start up, and<If>
are evaluated for each incoming request.To solve your problem, put your
$authenticationMode
variable into single quotes, because it is a string (as you have done for the right part):But what this macro will do is as follow:
At start up you have somewhere in your config the instruction
Use mymacro $param
The mod_macro doc says:
(* = The example in the doc is about virtualhosts)
So if you have
Use mymacro htpasswd
, it will expand to following config:This will test for each request some not very usefull conditions. And your server will never start because of the DO_CRASH instruction.
As alternative way, I would think about writing 2 macros: one corresponding to the authenticationMode == "htpasswd" and another for the authenticationMode == "server".
Please test and and take into account that I'm still learning Apache 2.4, so it may not be complete answer... Hope this helps!
A good read of Apache's expression doc could be useful too.