You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Pierre-Rudolf Gerlach <pg...@gmail.com> on 2012/04/10 17:15:45 UTC

[users@httpd] test against env variable previously defined by SetEnvIf

Hello,

I have a website hosted in a shared environment, so I only have access
to a .htaccess file to configure stuff. I'm running on Apache 2.4.1.

I would like to to something like
 - test the user-agent against a regexp and set a variable according to this
 - test if this variable is set and do something it it is

My problem is : although the variable is set, I can't figure out how
to test it in the <If> directive. I tried with %{ENV:myvar} and
env('myvar') (and other stuff that threw me errors)

Here's a sample .htaccess to demonstrate my problem :


# Set a value to a variable named "myvar"
SetEnvIf dummy .* myvar=value

<If "%{ENV:myvar} == 'value'">
SetEnvIf dummy .* myres=WIN
</If>
<If "env('myvar') == 'value'>
SetEnvIf dummy .* myres2=WIN
</If>
<If "-z %{ENV:myvar}">
SetEnvIf dummy .* myres3=myvarIsEmpty
</If>
<If "-z env('myvar')>
SetEnvIf dummy .* myres4=myvarIsEmpty
</If>

Header set test_safari "%{myvar}e"
Header set test_myvar "%{myres}e"
Header set test_myvar2 "%{myres2}e"
Header set test_myvar3 "%{myres3}e"
Header set test_myvar4 "%{myres4}e"


The response headers are :

Server:Apache/2.4.1 (Unix)
test_myvar:value
test_myres:(null)
test_myres2:(null)
test_myres3:myvarIsEmpty
test_myres4:myvarIsEmpty


How would I have a "WIN" printed ?


Thanks

-- 
Pierre-Rudolf Gerlach

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] test against env variable previously defined by SetEnvIf

Posted by Daniel Gruno <ru...@cord.dk>.
On 10-04-2012 18:59, Pierre-Rudolf Gerlach wrote:
> I just tried from a fresh checkout of branch 2.4.x (Apache 2.4.3-dev),
> using the shipped default config, and still myothervar is null.
> Same result with tags/2.4.2 ...
>
> Do you confirm you did just create a .htaccess with the content of my
> previous email ? If so, I guess I don't have any ideas left.
>
I must admit, I did not create a .htaccess, but put it in my httpd.conf
instead, which
is what makes it work. It would seem that somehow, in a .htaccess
context, the SetEnvIf
inside the <If> clause gets either ignored or...something else. I'll
have to get back to you
on that :)

With regards,
Daniel.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] test against env variable previously defined by SetEnvIf

Posted by Pierre-Rudolf Gerlach <pg...@gmail.com>.
On Tue, Apr 10, 2012 at 6:04 PM, Pierre-Rudolf Gerlach
<pg...@gmail.com> wrote:
> On Tue, Apr 10, 2012 at 5:49 PM, Daniel Gruno <ru...@cord.dk> wrote:
>> I have tried inputting exactly what you wrote into my configuration, and
>> the output I got was:
>>
>>  HTTP/1.1 200 OK
>>  ...
>>  myvar: value
>>  myothervar: othervalue
>>  Content-Type: text/html
>
> !!!
>
>> So it _should_ work, unless you have some other directives interfering
>> with it.
>> The only other explanation I can think of is that I'm using 2.4.2-dev
>> instead of 2.4.1, perhaps something changed since 2.4.1?
>
> I'm gonna download and install it to check.

I just tried from a fresh checkout of branch 2.4.x (Apache 2.4.3-dev),
using the shipped default config, and still myothervar is null.
Same result with tags/2.4.2 ...

Do you confirm you did just create a .htaccess with the content of my
previous email ? If so, I guess I don't have any ideas left.

-- 
Pierre-Rudolf Gerlach

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] test against env variable previously defined by SetEnvIf

Posted by Pierre-Rudolf Gerlach <pg...@gmail.com>.
On Tue, Apr 10, 2012 at 5:49 PM, Daniel Gruno <ru...@cord.dk> wrote:
> I have tried inputting exactly what you wrote into my configuration, and
> the output I got was:
>
>  HTTP/1.1 200 OK
>  ...
>  myvar: value
>  myothervar: othervalue
>  Content-Type: text/html

!!!

> So it _should_ work, unless you have some other directives interfering
> with it.
> The only other explanation I can think of is that I'm using 2.4.2-dev
> instead of 2.4.1, perhaps something changed since 2.4.1?

I have done a fresh install of apache 2.4.1 just to test this stuff,
so, unless there is something in the default config, I think the
problem is more likely to be a bug in Apache 2.4.1, corrected in your
version.

I'm gonna download and install it to check.

Thanks !

-- 
Pierre-Rudolf Gerlach

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] test against env variable previously defined by SetEnvIf

Posted by Daniel Gruno <ru...@cord.dk>.
On 10-04-2012 17:49, Daniel Gruno wrote:
> On 10-04-2012 17:36, Pierre-Rudolf Gerlach wrote:
>> My bad for the missing quote, I changed the values in the exemple
>> without re-testing it, sorry.
>>
>> You example works for a deny. However, if I slightly modify it,
>> replacing "Require all denied" by a variable setting, like this
>>
>> SetEnvIf dummy .* myvar=value
>> <If "env('myvar') == 'value'">
>>  SetEnvIf dummy .* myothervar=othervalue
>> </If>
>> Header set myvar %{myvar}e
>> Header set myothervar %{myothervar}e
>>
>> I get the problem : "myothervar" is not set. (null in the response
>> headers). I don't really get what difference it would make ?
>>
>> Thanks, regards
>>
I obviously needed to think outside the box for a bit before it hit me:
This is 2.4, we can use SetEnv!

SetEnvIf dummy .* myvar=value
<If "env('myvar') == 'value'">
 SetEnv myothervar othervalue
</If>
Header set myvar %{myvar}e
Header set myothervar %{myothervar}e

This should work very well within a .htaccess context.
What's probably best, for future reference, is if you try to solve
things using <If> to match a criteria,
followed by SetEnv to set the variables, so you don't have to rely on
SetEnvIf.

With regards,
Daniel.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] test against env variable previously defined by SetEnvIf

Posted by Daniel Gruno <ru...@cord.dk>.
On 10-04-2012 17:36, Pierre-Rudolf Gerlach wrote:
>
> My bad for the missing quote, I changed the values in the exemple
> without re-testing it, sorry.
>
> You example works for a deny. However, if I slightly modify it,
> replacing "Require all denied" by a variable setting, like this
>
> SetEnvIf dummy .* myvar=value
> <If "env('myvar') == 'value'">
>  SetEnvIf dummy .* myothervar=othervalue
> </If>
> Header set myvar %{myvar}e
> Header set myothervar %{myothervar}e
>
> I get the problem : "myothervar" is not set. (null in the response
> headers). I don't really get what difference it would make ?
>
> Thanks, regards
>
I have tried inputting exactly what you wrote into my configuration, and
the output I got was:

  HTTP/1.1 200 OK
  ...
  myvar: value
  myothervar: othervalue
  Content-Type: text/html

So it _should_ work, unless you have some other directives interfering
with it.
The only other explanation I can think of is that I'm using 2.4.2-dev
instead of 2.4.1, perhaps something changed since 2.4.1?

With regards,
Daniel.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] test against env variable previously defined by SetEnvIf

Posted by Pierre-Rudolf Gerlach <pg...@gmail.com>.
On Tue, Apr 10, 2012 at 5:25 PM, Daniel Gruno <ru...@cord.dk> wrote:
> I tested an If-clause on my 2.4 using the following example:
>
> SetEnvIf dummy .* myvar=value
> <If "env('myvar') == 'value'">
>  Require all denied
> </If>
>
> And lo and behold, I got access denied, so it should be working as
> intended, provided you fix your type :)

My bad for the missing quote, I changed the values in the exemple
without re-testing it, sorry.

You example works for a deny. However, if I slightly modify it,
replacing "Require all denied" by a variable setting, like this

SetEnvIf dummy .* myvar=value
<If "env('myvar') == 'value'">
 SetEnvIf dummy .* myothervar=othervalue
</If>
Header set myvar %{myvar}e
Header set myothervar %{myothervar}e

I get the problem : "myothervar" is not set. (null in the response
headers). I don't really get what difference it would make ?

Thanks, regards

-- 
Pierre-Rudolf Gerlach

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] test against env variable previously defined by SetEnvIf

Posted by Daniel Gruno <ru...@cord.dk>.
On 10-04-2012 17:15, Pierre-Rudolf Gerlach wrote:
> Hello,
>
> I have a website hosted in a shared environment, so I only have access
> to a .htaccess file to configure stuff. I'm running on Apache 2.4.1.
>
> I would like to to something like
>  - test the user-agent against a regexp and set a variable according to this
>  - test if this variable is set and do something it it is
>
> My problem is : although the variable is set, I can't figure out how
> to test it in the <If> directive. I tried with %{ENV:myvar} and
> env('myvar') (and other stuff that threw me errors)
>
> Here's a sample .htaccess to demonstrate my problem :
>
>
> # Set a value to a variable named "myvar"
> SetEnvIf dummy .* myvar=value
>
> <If "%{ENV:myvar} == 'value'">
> SetEnvIf dummy .* myres=WIN
> </If>
> <If "env('myvar') == 'value'>
> SetEnvIf dummy .* myres2=WIN
> </If>
> <If "-z %{ENV:myvar}">
> SetEnvIf dummy .* myres3=myvarIsEmpty
> </If>
> <If "-z env('myvar')>
> SetEnvIf dummy .* myres4=myvarIsEmpty
> </If>
>
> Header set test_safari "%{myvar}e"
> Header set test_myvar "%{myres}e"
> Header set test_myvar2 "%{myres2}e"
> Header set test_myvar3 "%{myres3}e"
> Header set test_myvar4 "%{myres4}e"
>
>
> The response headers are :
>
> Server:Apache/2.4.1 (Unix)
> test_myvar:value
> test_myres:(null)
> test_myres2:(null)
> test_myres3:myvarIsEmpty
> test_myres4:myvarIsEmpty
>
>
> How would I have a "WIN" printed ?
>
>
> Thanks
>
You seem to have an error in your second If-statement (a missing quote).
I tested an If-clause on my 2.4 using the following example:

SetEnvIf dummy .* myvar=value
<If "env('myvar') == 'value'">
 Require all denied
</If>

And lo and behold, I got access denied, so it should be working as
intended, provided you fix your type :)

With regards,
Daniel.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org