Possible Duplicate:
Need custom Nginx regex

Desired outcome is this:


to this:


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

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

What am I doing wrong here?

  • You're wrong in assuming that the rewrite string matches GET arguments. – VBart Jul 23 '12 at 21:40

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


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.)

| improve this answer | |
  • I already use try_files, just can't get it working with try_files. Your example just gives me a error 500 page. I have rephrased my question here: serverfault.com/questions/410522/need-custom-nginx-regex – RoboTamer Jul 24 '12 at 5:35
  • Sorry, I realized after I slept on it that try_files wouldn't work. The second block should have been fine, though. In any case, it's bad form to post duplicate questions; if you need to clarify your question you can just edit the original question. – Michael Hampton Jul 24 '12 at 15:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.