Saturday, October 30, 2021

[SOLVED] Apache: "Virtualhost not allowed here" error when using If condition

Issue

I have a condition inside httpd.conf that looks like this:

<If "-f '/some/path/to/existing/file'">
   <VirtualHost _default:$port1>
     ServerName example.com

     ...config1...
   </VirtualHost>
</If>
</Else>
   <VirtualHost _default:$port1>
     ServerName example.com

     ...config2...
   </VirtualHost
</Else>

However, it's giving me a syntax error AH00526 that says "VirtualHost not allowed here". If I comment out the "If Else" condition, and put a VirtualHost outside the block, it works.

How could I fix this while keeping the condition?


Solution

You could try the following. Instead of using If directives (which are really intended for checking request parameter, you could try using the IfFile directive.

IfFile checks if a file exists at startup. It has no else option, so you will need to make sure your file checks are exclusive for each server.



Answered By - Exelian