Search code examples
apache.htaccessserverconfigurationconfig

How to use <If> <ElseIf> <Else> in Apache with custom variables?


Define FOO "/bar/foo/bar_v1.0.0"

<If "${FOO} == '/bar/foo/bar_v1.0.0'">
    Define BAR2 "foofoo1"
</If>
<ElseIf "${FOO} == '/bar/foo/bar_v2.0.0'">
    Define BAR2 "foofoo2"
</ElseIf>
<Else>
    Define BAR2 "foofoo3"
</Else>

What I do wrong? I've read the Apache docs and this one too, but I can't see the problem.

• I've tried '${FOO}', ${FOO}, '%{FOO}', %{FOO}.

• I've tried to remove quotes from the string: <If "${FOO} == /bar/foo/bar_v1.0.0">

• I've tried to remove double quotes at all: <If ${FOO} == '/bar/foo/bar_v1.0.0'>

• I've also tried <If "${FOO} =~ /bar_v1\.0\.0/"> and

<If "${FOO} -strmatch '/bar/foo/bar.v1.0.0'">

• I've also tried quote marks removed from the values that are set with the Define directive:

Define FOO /bar/foo/bar_v1.0.0

• I've tried to remove ElseIf block, and use just If and Else

And I always get BAR2 defined as a "foofoo3", cuz it just an exception (default) value I put in "else", just in case. So first two block never work no matter what.

I don't get it, can I even use custom variables in Apache expressions? The code is located in Apache config file.


I'm getting Apache error and it doesn't start. The error is because I use BAR2 as a part of a path to load module. Of course Apache says it cannot load module, because actually I do not have such path "/usr/foofoo3/some_module" but I certainly have "/usr/foofoo1/some_module" and "/usr/foofoo2/some_module", but as I said first 2 blocks never work


Solution

  • Yesterday I also tried to do same tests with Numbers instead of String and had the same result. So, I was confused, disappointed and actually done with it. I've just figured out another way, it's not so beautiful in my view, but I work with what I have. If someone interested here's the code, it gives me exactly what I need: (there's no "foofoo3" at all, as I said it was an exception, it was for testing, and I tested this code with BAR2 value as "foofoo3" too, in DEFAULT section)

    Define BAR__v1_0_0 "OK"
    
    # CONDITIONAL
    <IfDefine BAR__v1_0_0 >
        Define FOO "/bar/foo/bar_v1.0.0"
        Define BAR2 "foofoo1"
    </IfDefine>
    <IfDefine BAR__v2_0_0 >
        Define FOO "/bar/foo/bar_v2.0.0"
        Define BAR2 "foofoo2"
    </IfDefine>
    
    # DEFAULT
    <IfDefine !FOO>
        Define FOO "/bar/foo/bar_v1.0.0"
        Define BAR2 "foofoo1"
    </IfDefine>
    

    Still, I was curious about this matter, especially what @airtower-luna said on github. So I did another super-simple test:

    <If "'word' == 'word'">
        Define BAR2 "foofoo1"
    </If>
    <ElseIf "'noword' == 'word'">
        Define BAR2 "foofoo2"
    </ElseIf>
    <Else>
        Define BAR2 "foofoo3"
    </Else>
    

    I did this too <If "word == word">

    What is the result? The same 😆!!! And I think this is ridiculous! I understand that config file it's not a script, but Apache guys made IfDefine work at startup, but not a simple-well-known for any coder If Else... I don't know what to say, only - well, ok then (I’m not saying that with meaning/intention to disrespect someone). So, maybe I'll try someday the @mike-kaminski solution, but not today. After all attempts I just really doubt it works at startup at all, as was [said here]