Using server variables

Topics: Developer Forum
Sep 10, 2007 at 12:15 PM
Edited Aug 14, 2008 at 1:27 PM
Is it possible to use server variables in rewritecond?

For example: RewriteCond %{QUERY_STRING} ^.*=http://(?!HTTP_HOST).*$ [I]

I however can't seem to get it working, so I'm wondering is IIRF designed to be able to do that?
Aug 14, 2008 at 1:03 PM
*bump* ?
Coordinator
Aug 14, 2008 at 10:39 PM

Wow, an old  question.  Sorry it languished.

It is possible to use server variables in the Test string, with the %{VARIABLE} syntax.

It is not currently possible to use server variables in the comparison pattern. 
Any use of %{SERVER_VARIABLE} in the comparison pattern is not currently expanded.
I think this might be an interesting feature to add. 

Coordinator
Aug 14, 2008 at 11:09 PM

More on that.

The regex pattern - the 2nd field on the RewriteCond line - is compiled at the time the pattern is read in. Therefore, it is not possible to do server variable expansion in that pattern.  To change that, we would need to defer the compilation of the regex until the time of evaluation, for each URL request.  That would have significant impact on performance of the filter. 

Of course, it would be possible to be smart about it and only defer compilation of the regex where necessary:  where a server variable reference is found within the pattern.  You would pay the cost only when necessary.

You may be able to get what you want by using a set of chained RewriteCond's.  For example, one which tests HTTP_HOST, and another that tests QUERY_STRING. Something like this:

 

   RewriteCond %{HTTP_HOST} ^(ghzdf0([0-9][0-9]))$ 
   RewriteCond %{QUERY_STRING} ^.*=http://(?!(ghzdf0([0-9][0-9]))).*$ [I]
   RewriteRule .... 

(There is an implicit AND between the RewriteCond lines. )

Aug 15, 2008 at 7:16 AM
Many thanks for the update. Now at least I know for sure I wasn't doing it wrong ;). In my particular case using a set of chained RewriteCond's isn't usefull because of the huge amounth of http_hosts i'd have to setup (which I'm guessing is a more significant impact on performance). Also setting up one dynamic rule and never having to look back to it is a nice pro. If you'd be willing to try and add this feature i'd be thrilled.