nginx rewrite rules conditional once again

Igor Sysoev igor at sysoev.ru
Tue Jun 29 19:44:25 MSD 2010


On Tue, Jun 29, 2010 at 05:27:20PM +0200, Malte Geierhos wrote:

>  Hi List,
> 
> I've got to convert some Apache Rewrite Rules to work with nginx.
> And i got kind of stuck in between how to solve this.
> 
> The old rewrite rule is like this :
> 
>       RewriteCond %{REQUEST_URI} !/[0-9]+$
>       RewriteCond %{REQUEST_URI} ^/(articles|people)/favorites
>       RewriteRule   ^(articles|people)/(.*)/$  /$1/$2/1/25  [R=301,L]
>      
>       ### /fragen/beliebte
>       RewriteCond %{REQUEST_URI} !/[0-9]+$
>       RewriteCond %{REQUEST_URI} ^/(articles|people)/favorites
>       RewriteRule   ^(articles|people)/(.*)  /$1/$2/1/25  [R=301,L]
> 
> so basically its catching those with ^/articles/favorites/what and
> ^/articles/favorites/
> and append /1/25 and most important - ignore when there is already /2/40
> or whatever.
> 
> At first i was looking into solving it with a location with something
> like :
> 
> location ~* ^/(articles|people)([^/]*)$ {
>     rewrite ^/(articles|people)/(.*)$  /$1/$2/1/25;
> }
> 
> but this did not work out as expected.
> My next idea was to try to catch $2/$3 and see if its a number
> 
> like :
> 
> location ~* ^/(articles|people)/(.*)$ {
>          if ( $request_arg !~  [0-9]) {
>                rewrite ^/(articles|people)/(.*)$  /$1/$2/1/25;
>         }
> }
> 
> hm but now i'm stuck.
> Anyone got an idea ?

You think in a backward logic. Try a forward logic: what should be done for
/articles/favorites/1/25
and
/articles/favorites/2/40
?

     location ~ ^/(articles|people)/[0-9]/[0-9]$ {
         ...
     }

     location ~ ^/(articles|people)/(.*)$ {
         rewrite ^/(articles|people)/(.*)$  /$1/$2/1/25   permanent;

         # or 0.8.42+:
         #return  301  http://$1/$2/1/25;
     }


-- 
Igor Sysoev
http://sysoev.ru/en/



More information about the nginx mailing list