I currently have an nginx config that enables requests for files such as "/contact.php" to be made with just "/contact". And I found a solution for redirecting any .php request to its friendly counterpart, however, I thought there may be a more elegant solution.
Is it possible to 403 or 404 requests made to a URI such as "/articles/index.php" or "/articles/index" (note that friendly URI rewrite is enabled) and only accept requests via "/articles/" which would still load the "/articles/index.php" file?
Basically, I want any "/index" or "/index.php" in any dir, or .php request to 403 or 404, and only accept the friendly extensionless request or directory root / to load index.php (without it being requested in the URI).
Is this possible? I tried something like this in my config to deny .php requests, but since there is technically a rewrite, it will just deny all requests. It is not currently in there as it did not work.
Current config:
location / {
try_files $uri $uri/ @extensionless-php;
index index.php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ /includes/(.+)\.php$ {
deny all;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}