Home > Article > Backend Development > How to Remove .php and .html Extensions from URLs Using NGINX?
Introduction
When serving web content, it can be desirable to hide file extensions from the URL for aesthetic or security reasons. This can be achieved using NGINX, a popular web server software.
Problem
The goal is to remove both .php and .html extensions from URLs while maintaining their functionality. For example, the URL http://www.mydomain.com/indexhtml.html should be displayed as http://www.mydomain.com/indexhtml, and http://www.mydomain.com/indexphp.php should be displayed as http://www.mydomain.com/indexphp.
Solution
The following NGINX configuration can be used to achieve the desired result:
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 ^(.*)$ .php last; }
Explanation
By implementing this configuration, both .php and .html extensions will be automatically removed from URLs, providing a clean and user-friendly browsing experience.
The above is the detailed content of How to Remove .php and .html Extensions from URLs Using NGINX?. For more information, please follow other related articles on the PHP Chinese website!