Having tried all the solutions on Stack, I was not able to remove the .php extension.
I managed to make the URLs accessible: www.mydomain.com/login but the user can always change the URL to www.mydomain.com/login.php and I’d like to avoid that.
Here is a part of my config :
JavaScript
x
location / {
try_files $uri $uri.html $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ .php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
Advertisement
Answer
This might be what you’re looking for
JavaScript
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
For .html
extensions you just have to add this
JavaScript
RewriteRule ^([^.]+)$ $1.html [NC,L]
If you want to add a trailing slash at the end you can ignore the code above and insert the code below.
JavaScript
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
If you’re using GoDaddy you need to enable MultiViews
at the top of your .htaccess
file
JavaScript
Options +MultiViews