I am trying to set up my first website with nginx and I cannot seem to make extensionless php pages to work.
I would like my pages like /aboutme.php
to appear as /aboutme
or I would like to just change my URLs to /aboutme
instead of /aboutme.php
and make those work.
I managed to make the last scenario work on CPanel hosting by editing .htaccess
like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
However, if I convert .htaccess
code to nginx using online tools, it doesn't work. I also tried to edit my nginx.conf
with try_files
and extensionless-php
with no success, I get 404. My plain nginx.conf
without try_files
and extensionless-php
looks like this:
server {
listen localhost:8081;
server_name mywebsite.com; # I use my website here
root /usr/share/nginx/web/website1;
location / {
root /usr/share/nginx/web/website1;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/web/website1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Edit: my question got marked as a duplicate, so I tried to configure it as it was said with no success, if I visit '/aboutme' I get '404 not found', if I visit '/aboutme.php', it's opened as '/aboutme.php' with '.php':
server {
listen localhost:8081;
server_name mywebsite.com;
root /usr/share/nginx/web/website1;
location / {
try_files $uri $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}