How to serve .html files without showing the .html extension?

Hello,

My Caddy server is working really well, it’s a delight to use and setup. Thanks to the maintainers for building this!

I’m simply serving static files, all ending in the .html extension.

Not unexpectedly, the .html extensions show up in the URL in the browser’s address bar.

However, it would be nice if I could call up my files in the URL without the .html extension, and also not show the .html extension after the file is loaded.

Example static file name:
newsletter.html

Should be callable as and show as:
some_domain.com/newsletter

I’ve looked through the docs (rewrite, uri, request matchers) but couldn’t quite figure it out yet.

Edit: My Caddy version is v2.5.2.

Thank you very much for any help.

If you can read go code. This has a solution:

@gsmlg I’m pretty sure they aren’t looking for a plugin/code solution.

@dbi Next time, please fill out the help topic template, as per the forum rules.

I think you’re looking for the try_files directive to do this.

For example:

example.com {
	root * /srv
	try_files {path}.html
	encode gzip
	file_server
}

What this will do is attempt to rewrite the request {path} to append .html if the file exists on disk inside of root. If a file with .html doesn’t exist, it does nothing and lets file_server do its thing (serve a file as normal if it does exist, or a 404 if it doesn’t, etc).

4 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.