# Rewrite

**URL:** <https://www.ruby-forum.com/t/rewrite/179588>\
**Category:** NGINX\
**Created:** [November 29, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/rewrite/179588 "2009-11-29T19:21:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [November 29, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/rewrite/179588/1 "2009-11-29T19:21:36Z")

</div>

Hello,

I wanted to rewrite this url

rss.xml?type=popular&cid=8  
to  
/cache/rss/popular\_8.xml

so I wrote

```
            location ~* \.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
                    root /home/site/public_html;
                    access_log off;
                    expires 30d;

                    rewrite ^rss\.xml?type=popular&cid=(.*)$ 

```

/cache/rss/popular\_8.xml break;  
}

But it doesn’t work 😕  
Can anyone help me out with this ? let me know what I’m doing wrong

Posted at Nginx Forum:

> **[Rewrite](https://forum.nginx.org/read.php?2,26530,26530#msg-26530)**
>
> Hello, I wanted to rewrite this url rss.xml?type=popular&cid=8 to /cache/rss/popular\_8.xml so I wrote location ~\* \\.(jpg|jpeg|gif|css|png|js|ico|xml)$ { root /home/site/publi

---

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [November 29, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/rewrite/179588/2 "2009-11-29T21:54:21Z")

</div>

Thank you Igor !!!

One more thing, is there a way to put there regular expressions,  
something like

location = /rss.xml {  
if ($args ~ ^type=popular&cid=([0-9]+) {  
rewrite ^ /cache/rss/popular\_$1.xml break;  
}  
}

Posted at Nginx Forum:

> **[Re: Rewrite](https://forum.nginx.org/read.php?2,26530,26561#msg-26561)**
>
> Hello, I wanted to rewrite this url rss.xml?type=popular&cid=8 to /cache/rss/popular\_8.xml so I wrote location ~\* \\.(jpg|jpeg|gif|css|png|js|ico|xml)$ { root /home/site/publi

---

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [November 29, 2009, 11:04pm UTC](https://www.ruby-forum.com/t/rewrite/179588/3 "2009-11-29T23:04:36Z")

</div>

Hello!

On Sun, Nov 29, 2009 at 03:53:49PM -0500, thinkbot wrote:

> Thank you Igor !!!
> 
> One more thing, is there a way to put there regular expressions, something like
> 
> location = /rss.xml {  
> if ($args ~ ^type=popular&cid=([0-9]+) {  
> rewrite ^ /cache/rss/popular\_$1.xml break;  
> }  
> }

```
location = /rss.xml {
    if ($args ~ "^type=popular&cid=([0-9]+)") {
        set $cid $1;
        rewrite ^ /cache/rss/popular_$cid.xml break;
    }
}

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [November 29, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/rewrite/179588/4 "2009-11-29T23:47:55Z")

</div>

I’ve made it like

location = /rss.xml {  
if ($args ~ ^type=popular&cid=([0-9]+)) {  
rewrite ^ /cache/rss/popular\_$1.xml break;  
}  
}

and get error: pattern “^” has less captures than referrenced in  
substitution “/cache/rss/popular\_$1.xml”

I guess its about rewrite ^, so where should I place ([0-9]+) to get the  
number ?  
I’ve tested with rewrite ^cid=([0-9]+) but doesn’t work

I’m still learning nginx, but it’s great web server

Thank you !

Posted at Nginx Forum:

> **[Re: Rewrite](https://forum.nginx.org/read.php?2,26530,26594#msg-26594)**
>
> Hello, I wanted to rewrite this url rss.xml?type=popular&cid=8 to /cache/rss/popular\_8.xml so I wrote location ~\* \\.(jpg|jpeg|gif|css|png|js|ico|xml)$ { root /home/site/publi

---

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [November 29, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/rewrite/179588/5 "2009-11-29T23:47:55Z")

</div>

ohh just noticed post by Maxim D., thank you !!!

Posted at Nginx Forum:

> **[Re: Rewrite](https://forum.nginx.org/read.php?2,26530,26595#msg-26595)**
>
> Hello, I wanted to rewrite this url rss.xml?type=popular&cid=8 to /cache/rss/popular\_8.xml so I wrote location ~\* \\.(jpg|jpeg|gif|css|png|js|ico|xml)$ { root /home/site/publi

---

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [November 30, 2009, 2:56pm UTC](https://www.ruby-forum.com/t/rewrite/179588/6 "2009-11-30T14:56:13Z")

</div>

One more thing,  
Is there a way to redirect from location rss.xml, to location / depends  
on the args ?

```
           location = /rss.xml {
                   root /home/site/public_html;
                   access_log off;
                   expires 10m;

                   if ($args ~ "^type=popular&cid=(\d+)") {
                           set $cid $1;
                           rewrite ^ /cache_sql/rss/popular_$cid.xml 

```

break;  
}

```
                   if ($args ~ "^type=search") {
                           #redirect to location /
                   }
           }

            location / {
                    root /home/site/public_html;
                    access_log off;
                    proxy_pass http://localhost:8080;
                    proxy_redirect off;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header Host $host;
                    proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;  
}

Posted at Nginx Forum:

> **[Re: Rewrite](https://forum.nginx.org/read.php?2,26530,26746#msg-26746)**
>
> Hello, I wanted to rewrite this url rss.xml?type=popular&cid=8 to /cache/rss/popular\_8.xml so I wrote location ~\* \\.(jpg|jpeg|gif|css|png|js|ico|xml)$ { root /home/site/publi

---

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [December 19, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/rewrite/179588/7 "2009-12-19T15:25:25Z")

</div>

On Sun, Nov 29, 2009 at 03:53:49PM -0500, thinkbot wrote:

> Thank you Igor !!!
> 
> One more thing, is there a way to put there regular expressions, something like
> 
> location = /rss.xml {  
> if ($args ~ ^type=popular&cid=([0-9]+) {  
> rewrite ^ /cache/rss/popular\_$1.xml break;  
> }  
> }

Yes. You may also use “\d+” instead of “[0-9]+”.

–  
Igor S.

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

---

<div class="post-metadata">

**Author:** ![thinkbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4bbf92/32.png) [@thinkbot](https://www.ruby-forum.com/u/thinkbot)\
**Post date:** [December 19, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/rewrite/179588/8 "2009-12-19T15:25:25Z")

</div>

On Sun, Nov 29, 2009 at 01:21:14PM -0500, thinkbot wrote:

> ```
> location ~* \.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
> root /home/site/public_html;
> access_log off;
> expires 30d;
> 
> rewrite ^rss\.xml?type=popular&cid=(.*)$ /cache/rss/popular_8.xml break;
> }
> 
> ```
> 
> But it doesn’t work 😕  
> Can anyone help me out with this ? let me know what I’m doing wrong

```
 root /home/site/public_html;

 location = /rss.xml {
     if ($args ~ ^type=popular&cid=8) {
         rewrite ^ /cache/rss/popular_8.xml break;
     }
 }

```

–  
Igor S.

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