I’m encountering an issue with Nginx where it is not consistently rewriting URLs to include the .php extension, even though I’ve set up the rewrite rules correctly. Specifically, I want to be able to access a page called resume.php by simply providing /resume in the URL (without the .php extension), but Nginx is not reliably doing this.
The current configuration works for some files but not all. Oddly, when I copy a non-working file to a new name (e.g., resume.php to resume2.php), it starts working as expected. I’ve tried multiple configurations, but I’m still not seeing consistent results.
Here’s the configuration I’m using:
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name icyrelic.com;
root /srv/IcyRelic;
index index.php;
rewrite_log on;
# Rewrite rule to append .php if no extension is provided
rewrite ^/(.*)/?$ /$1.php last;
rewrite ^/$ /index.php last;
# Handling root directory
location / {
try_files $uri $uri/ /index.php =404;
}
# PHP processing
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
include fastcgi_params;
}
# Deny access to hidden files
location ~ /.ht {
deny all;
}
}
Observations:
Some files work correctly when accessed without the .php extension, but others don’t.
When I copy a non-working file (e.g., resume.php to resume2.php), the new file works as expected without needing the .php extension in the URL.
I’ve verified the PHP-FPM service is running and the configuration is being reloaded.
What could be causing this inconsistency, and how can I ensure all PHP files are properly handled without needing the .php extension in the URL? I would also like for the files to load with the .php extension and as it stands currently none do.
When the URL is accessed without the .php extension (e.g., https://icyrelic.com/resume), instead of properly processing the request and serving the PHP page, Nginx attempts to download the file.
The contact page seems to work https://icyrelic.com/contact
