Question

How to configure URL extensions at Nginx

Posted January 23, 2021 173 views
Nginx

Hello,

I have a droplet and a website. I can upload my html files to my droplet and see them correctly.

But I just want to see my web pages like this;
12.34.556.708/home
12.34.556.708/about
12.34.556.708/test
etc

and not like this;
12.34.556.708/home.html
12.34.556.708/about.asp
12.34.556.708/test.php
etc

How can i do that? Is that possible with nginx settings?

I’ve tried a couple of configurations such as the one found on the link below –>

[https://www.digitalocean.com/community/questions/about-url-extensions-at-nginx](http://)

and also a few others i found on stack-overflow. But all don’t seem to be working.

Is there an actual means to load webpages without their extensions showing?

P.S I’m a newbie to nginx
Thank you.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @mxwlljr,

I think what you are looking for is the following

try_files $uri $uri/ $uri.html @extensionless =404;

Another possibility is @extensionless. @extensionless is treated like a normal file, and that’s because you’ve added an extra =404 after @extensionless within try_files – the @extensionless part would only work as the last parameter as an internal redirect to another context.

If you want not only to support handing requests without .html, but to also strip .php from any requests, you might want to do the following:

location / {
    if (-e $request_filename.html){
        rewrite ^/(.*)$ /$1.html;
    }
}
location ~ \.html$ {
    if ($request_uri ~ ^/([^?]*)\.html(\?.*)?$) {
        return 302 /$1$2;
    }
    fastcgi_...
}

Regards,
KFSys