3 messages in ru.sysoev.nginxRe: rewrite based on several and'ed c...
FromSent OnAttachments
Brice FigureauJun 19, 2007 12:32 am 
Aleksandar LazicJun 19, 2007 11:15 pm 
Ezra ZygmuntowiczJun 20, 2007 9:39 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: rewrite based on several and'ed conditions...Actions...
From:Ezra Zygmuntowicz (ezmo@public.gmane.org)
Date:Jun 20, 2007 9:39:21 am
List:ru.sysoev.nginx

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,

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-