How to include all php files in Nginx's rewrite rule? How to include all php files in Nginx's rewrite rule? nginx nginx

How to include all php files in Nginx's rewrite rule?


Possible solution:

location / {}location ~ /\.ht {    deny all;}location ~ ^/([0-9]+)($|/) {    rewrite ^/([0-9]+)/?$ /index.php?s=$1 last;    rewrite ^/([0-9]+)/(.*)\.php$ /$2.php?s=$1 last;    rewrite ^/([0-9]+)/(.*)$ /$2 last;}location ~ \.php$ {    try_files $uri =404;    ...}

The order of the last three location blocks is significant.

The include /etc/nginx/conf.d/php.inc; has been moved out of the location / block and inserted at the end, so it provides a top level location ~ \.php$ block rather than a nested location block. I show the contents of the include file in my example above, but your include statement would work exactly the same.

The location ~ ^/([0-9]+)($|/) block processes any URI that begins with an /{id} section. rewrite automatically appends the original query string to the rewritten URI. See this document for details.

You should include the try_files statement in your location ~ \.php$ block to avoid sending uncontrolled requests to PHP and return a 404 response if the PHP file does not exist.