Home > database >  nginx rewrite rules subfolder to file
nginx rewrite rules subfolder to file

Time:07-09

I have dynamic files on the server:

  • allen.php
  • bob.php
  • candy321.php
  • david.php

and I would like to add rewrite rules in Nginx, expected the URLs are:

I found a similar one, but I have no idea how to modify it to fit my case: Nginx Rewrite Location to subfolders

CodePudding user response:

You can try a modified version of this answer:

root /path/to/your/webroot;
index index.php index.html;

location / {
    try_files $uri $uri/ @extensionless-php;
}

location @extensionless-php {
    rewrite ^(.*)/$ $1.php last;
    rewrite ^ $uri.php last;
}

location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$uri;
    fastcgi_pass ... # path to your PHP-FPM socket file or listening IP address/port
}
  • Related