Search code examples
nginxurl-rewritingmultiviews

Nginx and try_flies not working properly


I'm trying to simulate Multiviews, but i have a problem with try_files. All is ok, but i want to rewrite all urls not managed to /index.php (but no with 404):

location ~ ^(/.+)/ {
   try_files $uri $uri/ $1.php?$args @extensionless-php /index.php;
}
location @extensionless-php {
   rewrite ^(.*)/ $1.php;
}

ALL urls are rewrited to /index.php. If i remove /index.php at the end of first location, everything is ok except the not managed url's (that i want to be rewrited to index). Any idea? Thank you


Solution

  • You may try to remove /index.php from first location, as you said. Then, to make it redirect to /index.php when no php file found, you may add a few lines into the location which handles .php requests:

    location ~ \.php$ {
        if (!-f $request_filename) {
            rewrite    .*    /index.php;
        }
        # your other directives such as fastcgi_pass 127.0.0.1:9000; etc.
    }