# Nginx rewrite rules conditional once again

**URL:** <https://www.ruby-forum.com/t/nginx-rewrite-rules-conditional-once-again/190869>\
**Category:** NGINX\
**Created:** [June 29, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/nginx-rewrite-rules-conditional-once-again/190869 "2010-06-29T17:27:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malte\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Malte\_G](https://www.ruby-forum.com/u/Malte_G)\
**Post date:** [June 29, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/nginx-rewrite-rules-conditional-once-again/190869/1 "2010-06-29T17:27:51Z")

</div>

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 ?

regards,  
Malte

---

<div class="post-metadata">

**Author:** ![Malte\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Malte\_G](https://www.ruby-forum.com/u/Malte_G)\
**Post date:** [June 29, 2010, 5:36pm UTC](https://www.ruby-forum.com/t/nginx-rewrite-rules-conditional-once-again/190869/2 "2010-06-29T17:36:03Z")

</div>

On Tue, Jun 29, 2010 at 10:27 PM, Malte G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hm but now i’m stuck.  
> Anyone got an idea ?

location ~\* ^/(articles|people)/([^0-9]+)$ { … }

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![Malte\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Malte\_G](https://www.ruby-forum.com/u/Malte_G)\
**Post date:** [June 29, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/nginx-rewrite-rules-conditional-once-again/190869/3 "2010-06-29T17:45:25Z")

</div>

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

> At first i was looking into solving it with a location with something
> 
> 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 S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Malte\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Malte\_G](https://www.ruby-forum.com/u/Malte_G)\
**Post date:** [June 30, 2010, 12:22pm UTC](https://www.ruby-forum.com/t/nginx-rewrite-rules-conditional-once-again/190869/4 "2010-06-30T12:22:50Z")

</div>

On Wed, Jun 30, 2010 at 03:44:44PM +0530, Rahul B. wrote:

> nginx.  
> But then one day, I just analyzed input used and output produced by apache  
> rules and in few hours I solved that problem is myself.
> 
> Switching from apache to nginx is as complex as switching form windows to  
> linux/mac.  
> If you start solving any problem with old knowledge you will always find  
> things difficult. 😉

This is not Apache issue, this is a wrong practice to configure anything  
with sendmail-style RewriteRules. BTW Apache is built by default without  
mod\_rewrite, you have to --enable-rewrite.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Malte\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Malte\_G](https://www.ruby-forum.com/u/Malte_G)\
**Post date:** [June 30, 2010, 12:15pm UTC](https://www.ruby-forum.com/t/nginx-rewrite-rules-conditional-once-again/190869/5 "2010-06-30T12:15:49Z")

</div>

> You think in a backward logic. Try a forward logic: what should be done for

Agree with this!

As a general rules, when writing nginx config, we better think from  
\*scratch  
\*.  
It happened to me that I came across some very complex apache rules and  
ended up sitting idle because I couldn’t find their direct conversion in  
nginx.  
But then one day, I just analyzed input used and output produced by  
apache  
rules and in few hours I solved that problem is myself.

Switching from apache to nginx is as complex as switching form windows  
to  
linux/mac.  
If you start solving any problem with old knowledge you will always find  
things difficult. 😉

-Rahul
