apache.htaccesssetenv

.htaccess setEnv as %{HTTP_HOST}


is it possible to do the htaccess SetEnv with %{HTTP_HOST}?

i try it like this:

SetEnv HOSTNAME %{HTTP_HOST}

but this doesen't work. i tested it with setting it like

SetEnv HOSTNAME localhost.work

and thats working. So it's just my configuration whats not really operating.

Hope someone can give me a quick hint why this isn't working.

Thanks in advance.


Solution

  • SetEnv (mod_env) is for setting simple hardcoded name/value pairs. You need to use SetEnvIf (mod_setenvif) if you want to set the var based on elements of the request.

    For example, to set the env var HOSTNAME to the value of the Host HTTP request header:

    SetEnvIf Host "(.*)" HOSTNAME=$1
    

    The SetEnvIf directive sets the environment variable conditionally... if the regex ((.*)) matches the the first argument (ie. the HTTP Host header) then it sets the env var.

    Host refers to the Host HTTP request header (the same as the HTTP_HOST server variable). (.*) is the capturing regex that this matches against and $1 is a backreference to this captured pattern.

    Reference:
    https://httpd.apache.org/docs/2.4/mod/mod_setenvif.html#setenvif