如何在Apache配置中的If-Else语句中使用自定义变量?

时间:2018-11-21 04:59:26

标签: apache httpd.conf

这是我的代码(由于不起作用,我已经将if-else部分注释掉了)

我的配置文件的片段/内容

/etc/httpd/conf.d/staff.vhost

Define host_domain "staff"

Include /etc/httpd/conf.d/stage_template.vhost

/etc/httpd/conf.d/stage_template.vhost

    #<If "${host_domain} == 'main'">
    #    ServerAlias stage.myhost.com
    #</If>
    #<Else>
        ServerAlias stage-${host_domain}.myhost.com
    #</Else>

$ {host_domain}可以有3个可能的值:main或staff或customer

我尝试过:

  • $ {host_domain}:我收到此错误

    AH00526:/etc/httpd/conf.d/stage_template.vhost的第9行出现语法错误:无法解析条件子句:语法错误,意外的T_OP_STR_EQ,期望为'('

  • %{host_domain}:我遇到另一个错误

    AH00526:/etc/httpd/conf.d/stage_template.vhost的第9行的语法错误: 无法解析条件子句:解析'%'附近的错误

谢谢!

编辑:我正在使用此page作为指导。

1 个答案:

答案 0 :(得分:1)

<If "false">
...
</If>

看看Expressions in Apache HTTP Server

编辑:

如果 host_domain 不是自创建的变量,则将所有大写字母${HOST_DOMAIN}设为

并用单引号将其包裹:

#<If "'${host_domain}' == 'main'">

看看this