rewrite based on several and'ed conditions...

Brice Figureau brice+nginx at daysofwonder.com
Tue Jun 19 11:32:28 MSD 2007


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>






More information about the nginx mailing list