This documentation contains instructions to strip filenames from the homepage (index), and filename extensions of other webpages, from Uniform Resource Locators (URLs) of a website hosted via Nginx. This results in a cleaner, easier-to-remember, and filename-extension-agnostic URL structure when the website is loaded by a web browser.
To strip index.<extension> from the homepage (index) URL, add the
following code to the location / block, nested within the server block, in
the global Nginx configuration file or website-specific Nginx configuration file:
if ($request_uri ~ "/index.<extension>") {
rewrite ^(|/(.*))/index\.<extension>$ /$2 permanent;
}
To strip index from the homepage (index) URL, add the following code to the
location / block, nested within the server block, in the global Nginx
configuration file or website-specific Nginx configuration file:
if ($request_uri ~ "/index") {
rewrite ^(|/(.*))/index$ /$2 permanent;
}
To strip <filename>.<extension> from the homepage
(index) URL, leaving only <filename>, add the following code to the
location / block, nested within the server block, in the global Nginx
configuration file or website-specific Nginx configuration file:
try_files $uri.<extension> $uri $uri/ =404;