# Having trouble understand how rewrite works

**URL:** <https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976>\
**Category:** NGINX\
**Created:** [October 19, 2011, 5:53pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976 "2011-10-19T17:53:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 19, 2011, 5:53pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/1 "2011-10-19T17:53:31Z")

</div>

Here’s a small sample that is not working:

server {  
# /index.cfm?PageID=1 → /  
location ^~ /index.cfm?PageID=1$ {  
rewrite ^ / permanent;  
}

```
    location / {
            try_files $uri @django;
    }

    location @django {
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;

```
            include uwsgi_params;
            if (!-f $request_filename) {
                uwsgi_pass staging;
                break;
            }
    }

```

}

It seems like the first directive is ignored when I request  
/index.cfm?PageID=1. The idea is to redirect some old links.

Thanks

–  
Victor Noagbodji  
[http://www.victorsreviews.com](http://www.victorsreviews.com)

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 6:28pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/2 "2011-10-19T18:28:10Z")

</div>

19.10.2011, 22:53, “victor” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Here’s a small sample that is not working:
> 
> server {
> 
> # /index.cfm?PageID=1 → /

- location ^~ /index.cfm?PageID=1$ {
- rewrite ^ / permanent;

- location = /index.cfm {
- 

```
            if ($arg_PageID = 1) {

```

- 

```
                    return 301 /;

```

- 

```
            }

```

- try\_files $uri @django;

> proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
> 
> include uwsgi\_params;

- 

```
            # 'if' is useless here

```

- if (!-f $request\_filename) {

> uwsgi\_pass staging;

- break;
- }

> [http://www.victorsreviews.com](http://www.victorsreviews.com)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
br, Denis F. Latypoff.

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 6:33pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/3 "2011-10-19T18:33:46Z")

</div>

Hi Dennis,

Thanks for your help,

If I understand correctly, I need to use $arg\_PARAMS. But in the full  
source, I have expressions like this one:

/index.cfm?PageID=(2|46)&(.\*)$

how do I deal with them in that case, if I can’t use regex on the  
query parameters?

Thanks

2011/10/19 Denis F. Latypoff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> - 
> 
> ```
> }
> 
> ```
> 
> > proxy\_set\_header X-Real-IP $remote\_addr;
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)
> 
> –  
> br, Denis F. Latypoff.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Victor Noagbodji  
[http://www.victorsreviews.com](http://www.victorsreviews.com)

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 6:38pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/4 "2011-10-19T18:38:05Z")

</div>

19.10.2011, 23:32, “victor” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi Dennis,
> 
> Thanks for your help,
> 
> If I understand correctly, I need to use $arg\_PARAMS. But in the full  
> source, I have expressions like this one:
> 
> /index.cfm?PageID=(2|46)&(.\*)$

map $arg\_PageID $is\_old {  
default 0;  
2 1;  
46 1;  
}

server {  
location = /index.cfm {  
if ($is\_old) {  
return 301 /;  
}  
# regular rules  
}  
}

> > > include uwsgi\_params;
> > 
> > –
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
br, Denis F. Latypoff.

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 6:40pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/5 "2011-10-19T18:40:34Z")

</div>

Sorry, index.cfm, of course, not index.php.

Andrejs

Posted at Nginx Forum:

> **[Re: Having trouble understand how rewrite works](https://forum.nginx.org/read.php?2,216917,216923#msg-216923)**
>
> Here's a small sample that is not working: server { # /index.cfm?PageID=1 --\> / location ^~ /index\\.cfm\\?PageID=1$ { rewrite ^ / permanent; } location / { try\_files $uri @djang

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 6:51pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/6 "2011-10-19T18:51:43Z")

</div>

Thanks again. I understand the map solution. Here’s another issue, you  
see, the links below all map to the index page (there are other pages  
too, that have those PageID, ParentPageID, etc.. parameters).

/index.cfm?PageID=1  
/index.cfm?resetCache=yes  
/index.cfm?PageID=1&ParentPageID=1&NavID=1  
/index.cfm?PageID=22&ParentPageID=1&NavID=22  
/index.cfm?PageID=14&ParentPageID=38&NavID=38&ReferringPage=1

all map to / (index)

/index.cfm?PageID=35&ParentPageID=26&NavID=26  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=8&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=9&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=10&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=15&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=16&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=11&PageText=off

and these for example would map to another unique page.

The parameters specify the actual page that must be mapped to. And  
since I now know I can’t use regex on query parameters, is there a way  
to handle this situation?

Thanks

On Wed, Oct 19, 2011 at 12:40 PM, locojohn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Victor Noagbodji  
[http://www.victorsreviews.com](http://www.victorsreviews.com)

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 6:51pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/7 "2011-10-19T18:51:43Z")

</div>

Thanks,

Please see reply to Denis.

On Wed, Oct 19, 2011 at 12:40 PM, locojohn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Victor Noagbodji  
[http://www.victorsreviews.com](http://www.victorsreviews.com)

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 6:39pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/8 "2011-10-19T18:39:57Z")

</div>

Hi!

Arguments cannot be checked with location directive. Put the below code  
into your main server {} block, for example, before declaring any of the  
locations:

```
            if ($request_uri ~ ^/index\.php\?PageID=1$) {
              rewrite ^ /? permanent;
            }

```

Andrejs

Posted at Nginx Forum:

> **[Re: Having trouble understand how rewrite works](https://forum.nginx.org/read.php?2,216917,216922#msg-216922)**
>
> Here's a small sample that is not working: server { # /index.cfm?PageID=1 --\> / location ^~ /index\\.cfm\\?PageID=1$ { rewrite ^ / permanent; } location / { try\_files $uri @djang

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 20, 2011, 2:20am UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/9 "2011-10-20T02:20:49Z")

</div>

On 19 Out 2011 17h51 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> all map to / (index)  
> Taking the lead from Denis, try:

map $request\_uri $is\_old {  
default 0;  
~/index.cfm?PageId=\d+ 1;  
~/index.cfm?PageID=\d+&ParentPageID=\d+&NavID=\d+ 1;  
~/index.cfm?PageID=\d+&ParentPageID=\d+&NavID=\d+&ReferringPage=\d+  
1;  
}

> /index.cfm?PageID=35&ParentPageID=26&NavID=26  
> /index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=8&PageText=off  
> /index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=9&PageText=off  
> /index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=10&PageText=off  
> /index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=15&PageText=off  
> /index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=16&PageText=off  
> /index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=11&PageText=off

map $request\_uri $is\_old\_also {  
default 0;  
~/index.cfm?PageID=\d+&ParentPageID=\d+&NavID=\d+&ProductID=\d+&PageText=off  
1;  
}

Then you use the same type of if in location like exemplified by Denis  
before. Just also a test for $is\_old\_also.

…

if ($is\_old\_also) {  
return 301 ;  
}

> and these for example would map to another unique page.
> 
> The parameters specify the actual page that must be mapped to. And  
> since I now know I can’t use regex on query parameters, is there a  
> way to handle this situation?

See above. Try it.

— appa

---

<div class="post-metadata">

**Author:** ![victor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3e96dc/32.png) [@victor](https://www.ruby-forum.com/u/victor)\
**Post date:** [October 19, 2011, 7:08pm UTC](https://www.ruby-forum.com/t/having-trouble-understand-how-rewrite-works/212976/10 "2011-10-19T19:08:25Z")

</div>

Ah sorry, this one was meant for you Denis, not sure how gmail handled  
it.

* * *

Thanks again. I understand the map solution. Here’s another issue, you  
see, the links below all map to the index page (there are other pages  
too, that have those PageID, ParentPageID, etc.. parameters).

/index.cfm?PageID=1  
/index.cfm?resetCache=yes  
/index.cfm?PageID=1&ParentPageID=1&NavID=1  
/index.cfm?PageID=22&ParentPageID=1&NavID=22  
/index.cfm?PageID=14&ParentPageID=38&NavID=38&ReferringPage=1

all map to / (index)

/index.cfm?PageID=35&ParentPageID=26&NavID=26  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=8&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=9&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=10&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=15&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=16&PageText=off  
/index.cfm?PageID=35&ParentPageID=26&NavID=35&ProductID=11&PageText=off

and these for example would map to another unique page.

The parameters specify the actual page that must be mapped to. And  
since I now know I can’t use regex on query parameters, is there a way  
to handle this situation?

2011/10/19 Denis F. Latypoff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> map $arg\_PageID $is\_old {
> 
> # regular rules
> 
> > > - 
> > > 
> > > ```
> > > }
> > > 
> > > ```
> > > 
> > > > proxy\_set\_header X-Real-IP $remote\_addr;
> > > > 
> > > > nginx mailing list  
> > > > Victor Noagbodji
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Victor Noagbodji  
[http://www.victorsreviews.com](http://www.victorsreviews.com)
