Removing .php and .html Extensions from URLs in NGINX
To resolve the issue of displaying clean URLs without the .php or .html extensions, you can implement the following configuration in your nginx configuration file.
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; }
This configuration achieves the desired result by first checking for the requested URI. If the URI exists, it is displayed as is. If it does not exist, it checks for the URI with the .html extension. Finally, if neither the URI nor the URI with the .html extension exists, it will rewrite the URI to add a .php extension and attempt to display that file.
By adding this configuration, Nginx will:
Once you have added this configuration to your file, restart Nginx, and your URLs should display cleanly without the extensions.
The above is the detailed content of How to Remove .php and .html Extensions from URLs in NGINX?. For more information, please follow other related articles on the PHP Chinese website!