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

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

Apache Web Server Forum

    
Environmental Variables in RewriteCond's
noyearzero




msg:4034284
 10:20 pm on Nov 30, 2009 (gmt 0)

Is it possible to use an environmental (or even a server) variable in a RewriteCond pattern?

these work
RewriteCond %{HTTP_HOST} ^myhost.com$
RewriteCond %{ENV:MYVAR} ^valueOfMyVar$

but how about
RewriteCond %{HTTP_HOST} ^%{ENV:MYVAR}$
or
RewriteCond %{ENV:MYVAR} ^%{HTTP_HOST}$

?

 

jdMorgan




msg:4034311
 11:42 pm on Nov 30, 2009 (gmt 0)

According to the Apache mod_rewrite documentation [httpd.apache.org], you can use any variable that has been defined in the current scope on the left side of a RewriteCond.

However, you cannot use any kind of variables on the "right side" of a RewriteCond.

If you're using later versions of POSIX regular expressions, you can use an "atomic back-reference" and the principle of commutativity to do a "compare." That is, if A+B is equal to A+A, then A=B. This applies to both numeric addition and to string concatenation.

However, this solution is not portable among servers with different OSes (and therefore different regex libraries), so it is recommended to call a script (e.g. PERL) using a RewriteMap if a general solution is required.

Jim

noyearzero




msg:4034599
 2:59 pm on Dec 1, 2009 (gmt 0)

Do you have an example of atomic backreferencing that you can post or link me to? The principle makes sense, but i'm not sure how to impliment it.

On the mod-rewrite page there is a line that says regarding enviromental variables... "Additionally you can dereference it in a following RewriteCond pattern via %{ENV:VAR}. Use this to strip but remember information from URLs." That led me to believe they can be used in the right side because the right side is the pattern side, correct?

jdMorgan




msg:4034697
 5:19 pm on Dec 1, 2009 (gmt 0)

Previously-defined variables may be de-referenced on the left side (Test String) of RewriteConds, or on the right side (Substitution) of RewriteRules.

You can go ahead and test the method you'd like to use, but it won't work. :(

Atomic back-references are discussed in many regular expressions tutorials. Take a look at this thread [webmasterworld.com] for a previous discussion of the subject in relation to variable comparison. Additional threads can be found using this search [google.com].

If you use this method, I strongly suggest that you put a 'permanent test rule' at the top of your code -- A rule that will cause the site to fail in an absolutely spectacular manner should your code every be ported to a server that doesn't support this function... or should your server get 'upgraded' to a server version and/or OS that doesn't support this function. I'd suggest comparing a variable to itself using the atomic back-reference, and if this fails, rewrite *all* requested URLs to an error page -- Something that will be immediately noticed, so the server move or upgrade can be aborted ASAP while you code a different solution. Document the hell out of this permanent test rule, so that it won't ever be accidentally/ignorantly removed!

Jim

noyearzero




msg:4039585
 6:35 pm on Dec 8, 2009 (gmt 0)

Ah thanks for those links, i understand whats going on now.

so if
RewriteCond hi<>hi ^(.+)<>\1$
evaluates to true, atomic back-references work on my server, correct?

also are the "<>" just a divider or do they have another special meaning?

jdMorgan




msg:4039706
 10:15 pm on Dec 8, 2009 (gmt 0)

Yes, that's a fair test.

"<>" are just "virtual" delimiters, so that we don't don't confuse variable-pair values such as "hi hi" and "hih i". This also serves as a 'visual delimiter' for you and whoever else might work on the code and wonder what on earth it does... :)

You could use ">" or "<->" or "~" -- any character or string that's very unlikely to appear in whatever variable you are testing.

Jim

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