nginx rewrite regex for API versioning

Posted by MSpreij on Server Fault See other posts from Server Fault or by MSpreij
Published on 2014-08-13T21:10:02Z Indexed on 2014/08/23 22:23 UTC
Read the original article Hit count: 464

Filed under:
|
|

What I want is for the first to be turned into the second..

/widget              => /widget/index.php
/widget/             => /widget/index.php
/widget?act=list     => /widget/index.php?act=list
/widget/?act=list    => /widget/index.php?act=list
/widget/list         => /widget/index.php?act=list
/widget/v2?act=list  => /widget/v2.php?act=list
/widget/v2/?act=list => /widget/v2.php?act=list
/widget/v2/list      => /widget/v2.php?act=list

v2 could also be v45, basically "v\d+"
act, "list" in this case, can have many values and more will be added.
Any additional query parameters would just be passed on with $args, I guess.

Basically URLs not specifying the version will go to index.php, which can then decide what specific version file to include. What I am afraid of happening is loops - this should sit in

location /widget {

right?.

(As for putting the version of the API in the URL, I'm not trying to be RESTful, and target audience is small)

Resources on how to do this entirely in index.php using "routers" also welcome :-/

© Server Fault or respective owner

Related posts about nginx

Related posts about rewrite