apachehttp-headersapache2apache2.4mod-headers

How to set Apache header only if environment variable has a certain value


I want to set a header in my Apache (2.4) config, but only if an environment variable has a certain value.

SetEnv ENV_NAME prod

How do I only set this header if ENV_NAME is not prod ?

Header set X-Robots-Tag "noindex, nofollow"

Solution

  • Header set X-Robots-Tag "noindex, nofollow" "expr=env('ENV_NAME') != 'prod'"
    

    From https://httpd.apache.org/docs/2.4/mod/mod_headers.html#header

    The Header directive may be followed by an additional argument, which may be any of:

    ...

    expr=expression The directive is applied if and only if expression evaluates to true. Details of expression syntax and evaluation are documented in the ap_expr documentation.