Need nginx regex rewrite debuging help

RoboTamer asked:

Possible Duplicate:
Need custom Nginx regex

Desired outcome is this:

http://www.example.com/about/?r=something

to this:

http://www.example.com/about.php/?r=something

This is what I got but it isn’t working:

if (!-e $request_filename)
{
    rewrite ^/([^?]*[^?/])/?(?.*)?$  /$1.php/$2  last;
    break;
}

What am I doing wrong here?

My answer:


You don’t need a regex for this at all. Just add it to your existing try_files:

Example:

try_files $uri.php $uri $uri/ @404;

If you aren’t (yet) using try_files, then convert soon, and in the meantime use something like this:

if (-e $request_filename.php) {
    rewrite ^(.+)$ $1.php last;
}

(I ripped this second example out of my notes from when I originally converted from Apache to nginx and wanted to have a URL /example load /example.php as Apache would do.)


View the full question and answer on Server Fault.

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