benrebia
By:
benrebia

How to remove .php using NGINX?

April 26, 2017 218 views
Nginx Ubuntu

http://example.com/Encrypt.php as http://example.com/Encrypt/
I've made configuration but i see Error There's No Page With This Name

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name IP;

location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;

   }

location /calculator {
                index index.php index.html index.htm;
                 if (!-e $fastcgi_script_name) {
                rewrite ^/calculator/(.+)?$ /calculator.php?url=$1 last;
         }
}

location /Encrypt {
                index index.php index.html index.htm;
                 if (!-e $fastcgi_script_name) {
                rewrite ^/Encrypt/(.+)?$ /Encrypt.php?do=$1 last;
         }


}


location /seo {
                 if (!-e $fastcgi_script_name) {
                rewrite ^/seo/(.+)?$ /seo.php?do=$1 last;
         }
 }
location /d65ez46dz {
                index index.php index.html index.htm;
                 if (!-e $fastcgi_script_name) {
                rewrite ^/d65ez46dz/(.+)?$ /d65ez46dz.php?do=$1 last;
         }

}

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}


It works BUT NO DATA ATT ALLLL
PLEASE ANY ONE HER FOR HELP THANK'S

1 Answer

The problem could be related with the "using if in nginx is evil". Which affects "if" when inside a location, mostly.

Instead of doing many "ifs" like that try to make one if and put all those 3 locations inside, if you really need that if at all.

To make the .php disappear use something like this:

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

Have another answer? Share your knowledge.