homepage Welcome to WebmasterWorld Guest from 54.196.201.253
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Why would SetEnv work when SetEnvIfNoCase fails?
in .htaccess, on a variable created in vhost file
Mike521




msg:4525653
 8:37 pm on Dec 6, 2012 (gmt 0)

I am trying to do mobile browser detection in our .htaccess file by setting an environment variable that will be picked up by our website later. We need to create the variable in the vhost file, otherwise it won't be available at all to our ecommerce system (although PHP scripts can see it).

So the variable is created in vhost and is visible to PHP and to our ecommerce system. Next step is to try to modify it with .htaccess. I can do a SetEnv with no problem, PHP shows the new value. But if I do a SetEnvIfNoCase, the value remains unchanged every time. I've verified that my case evaluates to true by setting a completely new variable and checking it with PHP - that variable is correctly set. So for example, assume the variable in vhost is called 'myOldVariable':


#Fails - does not modify the variable:
SetEnvIfNoCase User-Agent "chrome" myOldVariable=A

#Succeeds - creates a new variable with value 'A'
SetEnvIfNoCase User-Agent "chrome" myNewVariable=A

#Succeeds - modifies the variable
SetEnv myOldVariable=A

 

wilderness




msg:4525678
 8:58 pm on Dec 6, 2012 (gmt 0)

You need mod-rewrite to capture strings.

SetEnvIfNoCase User-Agent "chrome" myNewVariable=A


this line ends reading after the exactly as ("chrome") denying all browsers where the UA contains chrome and never reads the variable string.

Apache does solve some issues on its own, whether the result is what you desire, and/or to your liking, ia another story.
Surprising the line doesn't generate a 500 Error.

Mike521




msg:4525686
 9:11 pm on Dec 6, 2012 (gmt 0)

From what I can tell according to the documentation at apache.org, the regex for the user agent string can be enclosed in quotes, so I don't think it's doing an exact match, I think it's doing a regex.

Also, the comparison must be evaluating to true, because 'myNewVariable' is being created, and the comparison is exactly the same.

wilderness




msg:4525689
 9:18 pm on Dec 6, 2012 (gmt 0)

From what I can tell according to the documentation at apache.org, the regex for the user agent string can be enclosed in quotes, so I don't think it's doing an exact match,


Try the following two lines (not simultaneously) and you'll see that enclosing within quotes does exactly that

SetEnvIf User-Agent "chrome"

SetEnvIf User-Agent "Chrome"

The first will always fail because no Chrome browser uses the lower-case c.

The second will deny every Chrome visitor to your site (s).

I use quote very sparingly due to this conflict.

Mike521




msg:4525707
 10:43 pm on Dec 6, 2012 (gmt 0)

I don't understand. I was using SetEnvIfNoCase specifically to avoid case sensitivity issues. It must work, why else would 'myNewVariable' be created correctly?

lucy24




msg:4525715
 11:15 pm on Dec 6, 2012 (gmt 0)

Try the following two lines (not simultaneously) and you'll see that enclosing within quotes does exactly that

I think he meant a full-text match, not a case-sensitive match. SetEnvIf is case-sensitive unless you expressly tell it NoCase.

You can say BrowserMatch (or BrowserMatchNoCase) as a special shortcut for user agents. In fact I never bother with anything else.

Quotation marks are most useful for enclosing strings that contain spaces, since the space would otherwise have syntactic meaning:

BrowserMatch ^-?$ keep_out
BrowserMatch Ahrefs keep_out
BrowserMatch "America Online Browser" keep_out
BrowserMatch Clipish keep_out
BrowserMatch Covario keep_out
BrowserMatch CoverScout keep_out
BrowserMatch "Extreme Picture Finder" keep_out

et cetera. (Quick cut & paste from my shared htaccess).

wilderness




msg:4525733
 12:14 am on Dec 7, 2012 (gmt 0)

Quotation marks are most useful for enclosing strings that contain spaces, since the space would otherwise have syntactic meaning:



You mean like this:
SetEnvIf User-Agent " ; " keep_out
SetEnvIf User-Agent " \( " keep_out
SetEnvIf User-Agent "; " keep_out

lucy24




msg:4525741
 1:35 am on Dec 7, 2012 (gmt 0)

Yup. But watch out: I'd no sooner blocked UAs containing multi-spaces than I ran into a perfectly legitimate human UA that really did have two spaces. They're not all plainclothes bingbots, darn it.

SetEnvIf User-Agent "; " keep_out

I assume that one begin life as a double space before the Forums crunched it. You can protect multi-spaces in posts by using nonbreaking spaces, as in html-- but only as long as everyone reading the thread uses the same file encoding. Otherwise some folks will see   (A-circumflex with following nbsp) instead.

Now watch as this gets recursive. Only the first 16 Latin-1 characters have this feature.

Oh yes and: mod_setenvif uses Regular Expressions, so you can do package deals like " [);:]".

Mike521




msg:4525892
 1:54 pm on Dec 7, 2012 (gmt 0)

That's why I was surrounding the string in quotes - in my simple test it's not necessary but in reality it will be because the regex will have spaces.

OK so now that that's out of the way, I guess no one has any idea regarding the actual problem I'm having?

lucy24




msg:4525983
 7:46 pm on Dec 7, 2012 (gmt 0)

Where are vhost and htaccess located relative to each other? Are they physically in the same directory structure? If so, which one executes first? If I read your first post correctly, you've verified that htaccess is enabled and can use mod_setenvif. That was one essential step.

Mike521




msg:4525990
 8:04 pm on Dec 7, 2012 (gmt 0)

htaccess is definitely enabled and mod_setenvif must work also, because the new variable is successfully created with SetEnvIfNoCase

I'm not sure where they're located in reference to each other, or which runs first though. I imagine they are in the same directory structure.

I might have said vhost executes second, which would explain why the SetEnvIfNoCase attempt to modify the variable in .htaccess fails. But the SetEnv in .htaccess succeeds in modifying the variable, which implies .htaccess runs after vhost.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved