Home > OS >  NGINX/SWAG: Rewrite URL to remove .php extension
NGINX/SWAG: Rewrite URL to remove .php extension

Time:01-06

there are dozens of similar question in the internet (stackoverflow and other sites), but none of them worked for me. I am trying to rewrite my URL to get rid of the .php extension at the end. I already tried the common rewrites in my config. I hope somebody knows an answer i did not use yet.

CodePudding user response:

you can use .htaccess

make a new file in the directory which your PHP files are there, make a new file called .htaccess, put in the following in there:

RewriteCond %{THE_REQUEST} ^.*/*\.php
RewriteRule ^(.*).php$ /$1 [R=301,L]

CodePudding user response:

In your vhost config file or nginx.conf file, add the following lines in the inside server block :

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php; }

location ~ \.php$ {
    try_files $uri =404; }

location @extensionless-php {
    rewrite ^(.*)$ $1.php last; }

Need to restart Nginx before use.

  •  Tags:  
  • Related