rewrite based on several and'ed conditions...

Ezra Zygmuntowicz ezmobius at gmail.com
Wed Jun 20 20:39:44 MSD 2007


On Jun 19, 2007, at 12:32 AM, Brice Figureau wrote:

> Hi all,
>
> In Apache, I can decide to rewrite based on multiple conditions like:
> (totally stupid example ahead):
> RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$
> RewriteCond %{USER_AGENT} GoogleBot
> RewriteRule ^ /index.html [L]
>
> It doesn't seem to be possible under nginx except using this lame  
> trick:
>
> if ( $request_method ~ (GET|HEAD) ) {
> 	rewrite ^(.*)$ /get-or-head$1;
> 	break;
> }
>
> location ^~ /get-or-head/ {
> 	if ( $user_agent ~ GoogleBot ) {
> 		rewrite ^ /index.html last;
> 	}
> 	# back to normal
> 	rewrite ^/get-or-head(.*)$ $1 last;
> 	break;
> }
>
> Is there a better alternative ?
>
> If not, could it be possible to have the operator && and || defined in
> if() or at least leave the possibility to use interleaved if():
>
> either:
> if ( $request_method ~ ^(GET|HEAD)$ && $user_agent ~ GoogleBot ) {
> 	# do something
> }
>
> or
> if ( $request_method ~ ^(GET|HEAD)$ ) {
> 	if ( $user_agent ~ GoogleBot ) {
> 	# do somtething
> 	}
> }
>
> Thanks,
> -- 
> Brice Figureau <brice+nginx at daysofwonder.com>

	Nginx doesn't support nested if statements or && in conditionals.  
You can sort of fake it by doing something like this:


set $matcher "$request_method#$user_agent"

if($matcher ~ ^(GET|HEAD)#GoogleBot$) {
    # do something
}

Cheers-

-- Ezra Zygmuntowicz 
-- Lead Rails Evangelist
-- ez at engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)







More information about the nginx mailing list