Nginx 404 rewrite issue

Justin asked:

I’m trying to grab variables between slashes and pass them to my management script with the following rewrite line in Nginx:

rewrite ^/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ /index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5&p6=$6;

If I request the page with less than 6 variables (eg: /var1/var2/var3/var4/var5/) it 404s.

Is it possible to use this with less than 6 variables? (I will need the full 6 variables in different parts of my web application though)

Running CentOS 6 in OpenVZ with Nginx 1.4.1-1.el6 64bit + PHP-FPM.

Thanks in advance.

My answer:

If you don’t have exactly six items between slashes, then this pattern won’t match, and you’ll get a 404.

You can make five new rewrite rules containing one through five variables, respectively, (or someone who’s better at regex than I am might suggest a single rule to cover all six cases, but that’s above most people’s heads) or just handle this in your application instead.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.