Bootstrap FreeKB - Nginx (Web Server) - hide .php extension
Nginx (Web Server) - hide .php extension

Updated:   |  Nginx (Web Server) articles

This assumes you already have a web server (such as an HTTPD or Nginx web server) configured to produce PHP web pages. If not, check out my article on linking together an Nginx and PHP container running on Docker. In this scenario, your Nginx conf file could look something like this.

server {
    listen       80;
    server_name  stage.example.com;
    root         /var/www/html;
    index        index.html index.php;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
      root           html;
      fastcgi_pass   10.5.14.3:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
    }
}

 

By default, the .php extension will show in a web browsers address bar. For example, if you've a page named About.php, the address bar will show www.example.com/About.php.

 

Update the try_files directive in your main location block in your nginx.conf file to have @extensionless-php.

location / {
    try_files $uri $uri/ @extensionless-php;
}

 

Also add an @extensionless-php location block and restart nginx for this change to take effect.

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

 

Now, .php will no longer be displayed.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


September 28 2022 by freqnoiz
Did just that and the browser started to download all php files :D GREAT

Add a Comment


Please enter 971560 in the box below so that we can be sure you are a human.