# Query Rewrite Help

**URL:** <https://www.ruby-forum.com/t/query-rewrite-help/242659>\
**Category:** NGINX\
**Created:** [July 13, 2015, 12:17pm UTC](https://www.ruby-forum.com/t/query-rewrite-help/242659 "2015-07-13T12:17:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [July 13, 2015, 12:17pm UTC](https://www.ruby-forum.com/t/query-rewrite-help/242659/1 "2015-07-13T12:17:46Z")

</div>

Hi,

Am suffering with a bit of writer’s block when it comes to query  
rewrites.

I’ve got a rewrite rule that looks like :

rewrite ^/api/app/([^/]+)/([^/]+)/([^/]+)?$ /api/app/$1.php?p=$2&q=$3?  
last;

I am having two problems :

- I am getting 404 when I try to make a POST call to /api/app/login, I  
have to use the full suffix, i.e. api/app/login.php instead.

- I want to make the last ([^/]+) optional, but seem to also be getting  
the same 404 if I forget to include it ?

Ideas most welcome, thanks !

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [July 13, 2015, 1:06pm UTC](https://www.ruby-forum.com/t/query-rewrite-help/242659/2 "2015-07-13T13:06:16Z")

</div>

Your PCRE is not right. You might interpret it in more ‘natural’  
language  
as:  
/api/app/\<anything but ‘/’\>/\<anything but ‘/’\>/\<anything but ‘/’  
or  
nothing\>  
The URI /api/app/login does not match it.

You have several ways of addressing that:

- Rewrite you huge, do-it-all PCRE, adding optional, non-capturing  
groups
- Split the rewriting rule in several versions, each addressing one  
depth  
level (eases readibility/maintenance/performance?)

## I would also suggest you avoid using rewrite and replace it with location/return as much as you could: [https://youtu.be/YWRYbLKsS0I](https://youtu.be/YWRYbLKsS0I)

_B. R._

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [July 13, 2015, 3:39pm UTC](https://www.ruby-forum.com/t/query-rewrite-help/242659/3 "2015-07-13T15:39:19Z")

</div>

Thanks !
