Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Regarding the example of instead of hi.html just name the file hi and use the nginx Config “default_type text/html” —- is there an equivalent to this for other webservers (Eg Caddy etc) ? Perhaps it would be cleaner to just create a directory called hi with and index.html?


I usually create a "file filter" instead. Keep the extension as is (hi.html) and then use the try_files directive in nginx to make the server look for files with the same path using .html as an example. It has been a while since I used nginx but it looks something like this:

try_files $uri.html


Ah yes I remember stumbling on this years ago when I was digging on something. Thanks for the reminder!


That's that many static site generator do, use a folder and index.html for "clean urls".

Though personally I would do as the other comment said, keep it hi.html and use try_files or mod_rewrite to handle it. I mean, ask me to set up something like this and I probably would never have though of doing it the way in the article. It just seems weird to me.


I don't understand the benefit of this at all. I've been dropping the ".html" from URls for ages now, not by modifying the filename, but using Apache's MultiViews. Apart from anything else, some tools still benefit from a filename's extension advertising that file's type, even if they shouldn't.


Ah very interesting, just looked up multiviews, thanks!

Seems perhaps similar to try_files for nginx that a sibling commenter referenced?


With Caddy, keep your filename the same and use try_files:

    try_files {uri}.html {uri}
Done. (You can also add a redirect if you want to enforce that ONLY the non-.html version is used.)


Cool, thanks!


That Nginx config is so beautiful compared to Apache .htaccess configuration for the same result.


I usually create a "file filter" instead. Keep the extension as is (hi.html) and then use the try_files directive in nginx to make the server look for files with the same path using .html as an example. It has been a while since I used nginx but it looks something like this:

try_files $uri.html


Is it? Assigning text/html to extensionless file would be:

    <FilesMatch "^[^.]+$">
        ForceType text/html
    </FilesMatch>
Which I don't think is that bad, compared to RewriteRule's.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: