Adding multiple conditions in SetEnvIf
Asked Answered
A

2

8

I need to use SetEnvIf in my .htaccess file.

I also need to satisfy multiple conditions and then, show the required URL.

Here is my code:

SetEnvIf Remote_Host "^" press_flag=0
SetEnvIf Request_URI '/press/$' press_flag=1
SetEnvIf Cookie 'language\_uiAAAenglishBBB' press_flag=press_flag+1
SetEnvIf press_flag 2 Request_URI='Remote_Host/eng/test.html'

Explanation:

  1. In the first line, I am setting a variable press_flag to 0.
  2. Second line, I check whether the URL ends with this text: /press/, if true, the I set the 'press_flag' to 1.
  3. Third line, I check if the cookie matches language\_uiAAAenglishBBB text, if true, then I increment press_flag value by 1.
  4. Last line, I check if the press_flag value is 2, then I set the HTTP_HOST accordingly.

But, when I open URL/press/ in browser, it is not getting redirected.

Please help to debug and fix this code.

Thanks.

Anxious answered 1/7, 2013 at 10:28 Comment(5)
Second line does not make sense, HTTP_HOST is the domain name, has nothing to do with the path segment of the URL. And I don’t see you doing any redirecting anywhere.Macaw
@CBroe: Ok. Thanks for the correction. So, should I replace HTTP_HOST to HOST?Anxious
No, there is no environment variable called HOST – and if there was, HOST would still not mean “path”.Macaw
@CBroe: ok, great to know that. Can you please help me with this, so as to what changes to be made in order to achieve the target?Anxious
Should I replace HTTP_HOST with Request_URI ?Anxious
H
3

Whilst the answer provided by @akond satisfies a solution to the problem;

I do believe this is a good example of using the SetEnvIfExpr Directive in response to the original OP's question of "Adding multiple conditions in SetEnvIf":

SetEnvIfExpr "%{REQUEST_URI} =~ m#^/press/$# && %{HTTP_COOKIE} == 'language\_uiAAAenglishBBB'" Request_URI='Remote_Host/eng/test.html'

See more details at:

Hyperbolic answered 10/11, 2020 at 23:51 Comment(0)
M
2

I don't think you can redirect with SetEnvIf. You should use mod_rewrite instead:

RewriteCond %{REQUEST_URI} =/press/
RewriteCond %{HTTP_COOKIE} \blanguage=english\b
RewriteRule .* /eng/test.html [R]
  1. Checks if /press/ is requested
  2. Checks if the cookie is set to certain value.
  3. Redirection rule
Maggard answered 7/8, 2017 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.