How to change charset in Content-Type?

I host a website that has html files encoded using ISO-8859-1.
By default, Caddy adds the header

Content-Type: text/html; charset=utf-8

I can fix the problem for files named in the URL using

mime {
  .html "text/html; charset=ISO-8859-1"

But that does not work for the index.
When accessing domain/ instead of domain/index.html, the custom Content-Type set in mime above is not used.

How to change or remove the charset?

Howdy @andre, welcome to the Caddy community.

Caddy’s static fileserver doesn’t actually write the Content-Type header, it instead leverages the Go standard library to handle this while serving the file.

Here’s the behaviour of http.ServeContent:

If the response’s Content-Type header is not set, ServeContent first tries to deduce the type from name’s file extension and, if that fails, falls back to reading the first block of the content and passing it to DetectContentType.

http package - net/http -

This doesn’t look good for your scenario, though. There’s two things I can think of to try:

  1. Rewrite to a .html file in case mime picks up the rewritten extension

Because rewrite executes before mime does, you might be able to rename index.html to home.html or similar and put in a rewrite from the index to the HTML file:

rewrite {
  if {path} is /
  to /home.html

Renaming the file on disk away from index.html is necessary because the static fileserver will otherwise canonically redirect it back to /.

This may not work - I haven’t tested it - but I suspect mime will pick up the rewritten file and to your visitors it will simply work. If it doesn’t, the next option is…

  1. Rewrite internally to a folder you know nobody’s going to use

Move index.html to foo/index.html, then rewrite:

rewrite {
  if {path} is /
  to /foo/

Then add header /foo/ Content-Type "text/html; charset=ISO-8859-1" to override the Content-Type header for this entire area.

Unfortunately, both options look a bit ugly.

Would the mime directive help here?

Indeedily-do, but the issue appears to be that mime doesn’t operate without an extension, and when the index is requested, the extension is not part of the request and so mime does not operate. Even if an extension were part of the request - e.g. /index.html - the static fileserver would canonically redirect down to /, where mime doesn’t appear to be operating.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.