Forum Moderators: phranque
IP Date ResponseCode
Host Page Pre-redirectedPage(if relevant)
EnvVars (Name: Value:regex) (one per line)
HTTP headers (one per line)
accept: none
accept_lang: none
badua: old_browser:Chrome/66.
BlockCountry: 1
browser: chrome:Chrome/66
ips: amazon:18.237.
proto: too_low:HTTP/1.0
useragent: scrape:iodc
SetEnvIf Remote_Host 10.0.1.21 host=us:$0 (our IP, obfuscated)
<if " %{Remote_Host} =~ m#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}# ">
SetEnv host=numbers:$0
</if>
SetEnvIf Request_URI "\/\.?wp-" badrequri=wp:$0
...or its alternative...
<if " %{Request_URI} =~ m#wp-#i ">
SetEnv badrequri=wp:$0
</if>
How are you checking the environment var?By name. I first tried a {foreach} approach working straight from getenv, essentially the identical code to the function that gets any and all headers. But it dumped out every conceivable environmental variable--including the ones that simply repeat information I've already got--clogging up the file with information I didn't need and wasn't interested in. So instead I went to a function that starts with
if ($value)
# Request "/foo?question"
SetEnvIfExpr "%{QUERY_STRING} =~ /.+/" thisworks=$0
RewriteRule ^foo$ /bar?thisworks=@%{ENV:thisworks}@ [R,L]
A string like "0" (that evaluates to (bool)False) would also be excluded.Yes, that's essentially what I want. I can't currently think of any situation where it would be useful to know that suchandsuch variable has been set, but has a value of zero (or of the string "" in the case of a nonexistent capture). In this respect it's analogous to headers: for access-control purposes it makes no difference whether a given field is empty, or if it wasn’t sent at all.
So, in your findings, SetEnvIf can use the $0 etc. captures from the <If> envelope?
SetEnvIf Remote_Addr .* amazon ips=amazon:$0 SetEnvIfExpr "%{HTTP_COOKIE} =~ /.+/" cookie=notours:$0 SetEnvIfExpr "%{HTTP_COOKIE} =~ /(.+)/" cookie=notours:$0 query: inject:xx=md5 SetEnvIfExpr "%{HTTP_COOKIE} =~ /.+/" cookie=notours:$0Trala, more datapoints. The $0 without capture is the same thing that gave a value of 2 in the version I tried. Fortunately the lesson learned is straightforward: when in doubt, use parentheses!
...got an incorrect value for the cookie compared with the expected value. In fact in one case it returned ll(dot)org(dot)uk which were the final characters of the domain name; it should have been "humans-" something.
No, the SetEnvIf gets its value from WITHIN the <if>...Oops, right, gotcha. But at least SetEnvIf, unlike SetEnvIfExpr, doesn't demand parentheses in order to return an accurate value for $0.
SetEnvIf QUERY_STRING should behave the same as SetEnvIf REQUEST_URI. There are probably reasons for NOT doing that ...
SetEnvIfExpr "%{TIME_YEAR} =~ /.+/" test1=$0
SetEnvIfExpr "%{TIME_YEAR} =~ /(.+)/" test2=$0
RewriteRule ^foo$ /bar?test1=%{ENV:test1}&test2=%{ENV:test2} [R,L]
The Redirect trick is a real timesaver, since I can see it right there in my browser's address bar--and then I pulled up the logged headers and confirmed that I was getting the same test1 and test2 values each time.