In Apache Core there is the Define
, as documented: https://httpd.apache.org/docs/2.4/mod/core.html#Define
It is used to define variables
Define root_dir /var/www/meusite
<VirtualHost :80>
ServerName meusite.local
ServerAlias www.meusite.local
DocumentRoot ${root_dir}/public
ErrorLog ${root_dir}/__apache__.log
</VirtualHost>
On multiple hosts:
Define root_dir1 /var/www/meusite1
Define root_dir2 /var/www/meusite2
<VirtualHost meusite1.local:80>
ServerAlias www.meusite2.local
DocumentRoot ${root_dir1}/public
ErrorLog ${root_dir1}/__apache__.log
</VirtualHost>
<VirtualHost meusite2.local:80>
ServerAlias www.meusite2.local
DocumentRoot ${root_dir2}/public
ErrorLog ${root_dir2}/__apache__.log
</VirtualHost>
It is worth noting that if you use many "includes" maybe back and a half may accidentally end up conflicting, and include something twice, in this case you can use the <IfDefined>
to check whether a file or a variable has already been defined
<IfDefine !FOO>
Include Foo.config
</IfDefine>
In this clear example that within Foo.config
there must be something like:
Define FOO valor
In addition to setting it is possible to remove a variable, thus:
<IfDefine FOO>
UnDefine FOO
</IfDefine>
It is good to highlight this excerpt from the documentation:
Virtual Host Scope and pitfalls
While this Directive is supported in virtual host context, the changes
it makes Visible to any later Configuration Directives, Beyond any
enclosing virtual host.
Translating:
The scope of the Virtual Host and its pitfalls
Although this directive is supported in the context of the Virtual Host, the changes made are visible to any other later configuration directive in addition to any other included Virtual Host.
Curiosity: these variables are defined only in the scope of the virtual host file or it is global?
– Wallace Maxters
@Wallacemaxters unfortunately gets in the global scope. Updated answer.
– Guilherme Nascimento