2

I can't find an answer for my question.

Can I set an environment variable using another environment variable ?

Example:

SetEnv msg_1 "First message"
SetEnv msg_2 "%{ENV:msg_1} Second message"

To be clear, I don't want to use %{ENV:msg_1}%{ENV:msg_2}.

I wan't to use %{ENV:msg_2}.

Is that possible, cause doesn't seems to work to me ?

2 Answers 2

5

It is an interesting problem. SetEnv has very limited feature and scope.

You can use SetEnvIf here.

# set msg_1
SetEnvIf Host ^ "msg_1=First message"

# set msg_2 using msg_1
SetEnvIf msg_1 "(.*)" "msg_2=$1 Second message"

$1 is value we are capturing in group #1 which is (.*) (everything).

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, I'd overlooked that you can reference another env var in the SetEnvIf directive itself. ++
3
SetEnv msg_2 "%{ENV:msg_1} Second message"

You will need to use mod_rewrite (or SetEnvIf - See @anubhava's answer) instead in order to set an environment variable using the value of another env var. Unfortunately, SetEnv (mod_env) and SetEnvIf (mod_setenvif) do not expand the value of env vars using the syntax %{ENV:varname} in the resulting expression, it is simply seen as literal text.

However, if you use mod_rewrite then you can't use SetEnv to set your initial env var since it is processed too late. You will need to use either SetEnvIf or mod_rewrite to set the initial env var.

For example:

RewriteEngine on

SetEnvIf ^ ^ "msg_1=First message"

RewriteRule ^ - "[E=msg_2:%{ENV:msg_1} Second message]"

Note the strategically placed double quotes because the value argument contains spaces.

  • SetEnvIf - the entire name=value pair in the SetEnvIf directive must be surrounded in double quotes (not the value).

  • RewriteRule - the entire flags argument needs to be surrounded in double quotes (not the value).

This does allow you a bit more flexibility than SetEnvIf, since you can reference any number of env vars in the value portion if you need to.

1 Comment

I like this ^ ^ ++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.