Allen Chak
Allen Chak

Reputation: 1960

nginx rewrite rules subfolder to file

I have dynamic files on the server:

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

Upvotes: 0

Views: 62

Answers (1)

Ivan Shatsky
Ivan Shatsky

Reputation: 15687

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
}

Upvotes: 1

Related Questions