0

I've got the following in the .htaccess located in the root directory of website https://klipspringer.avadeaux.net/:

AddDefaultCharset UTF-8
AddCharset UTF-8 .js
AddCharset UTF-8 .html
AddCharset UTF-8 .java
AddCharset UTF-8 .c
AddCharset UTF-8 .css

This works fine for most code files that can be viewed in the browser. For instance, when viewing sendcmd.js in Chrome, the “–” character in “2014–2021” is correctly displayed: sendcmd.js correct display

But the UTF-8 directive doesn't seem to work for .java files. For instance, in TrackChain.java, Chrome displays the UTF-8 bytes of the “–” character as three characters: enter image description here

Any ideas why, or what can be done about it?

Safari has the same problem as Chrome (while Firefox doesn't want to show Java files at all, but prompts me for a downloading destination).

(Bonus question: Is there any way to set the charset for files that don't contain a ‘.’, like Makefile?)

UPDATE: The following worked for viewing Java and and Makefile in UTF-8:

<FilesMatch "\.java$">
     Header set Content-Type "text/x-java-source; charset=UTF-8"
</FilesMatch>
<FilesMatch "^[Mm]akefile$">
     Header set Content-Type "text/plain; charset=UTF-8"
</FilesMatch>

Why the AddCharset UTF-8 .java line has no effect is however still a mystery to me.

njlarsson
  • 2,128
  • 1
  • 18
  • 27
  • And you made sure that the browser actually receives what you expect? [How to check with Chromium based browsers](https://stackoverflow.com/a/56790467/4299358). – AmigoJack Sep 05 '21 at 09:26
  • When checking it, I found that – as expected – the browser did not receive any charset header for .java files, until I applied the fix in the update of the question. – njlarsson Mar 27 '22 at 16:31

1 Answers1

0

As per the documentation AddDefaultCharset affects all files of MIME type text/plain or text/html (which is also pointed out in a comment to htaccess UTF-8 encoding for .html, .css, .js - Whats the best way?). So your definition to .html is redundant (unless they're in fact XHTML and served as such).

AddCharset is part of mod_mime, so you have to make sure this module is available. As per documentation you can define multiple extensions with one line instead of using one line for each.

  • Requesting your JAVA example however only brings back this header, without any hint of the text encoding - unsurprisingly then the client either guesses it correctly or not:

    content-type: text/x-java-source

    So you have only assumed what is served, effectively skipping the possibility that you could still be part of the problem, and instead accused the clients for their behaviour.

  • Requesting your JS example brings back a header as expected:

    content-type: application/javascript; charset=UTF-8

So you have to find out why JAVA files are not served with any charset definition. See all answers in How to change the default encoding to UTF-8 for Apache to try a few workarounds and use your browser to check if the response headers actually contain what you want.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
  • I don't know why you think I blamed the clients. Obviously, the problem I am trying to address is on the server. Thanks for the link to the question with a variety of workarounds in the answers – most of them had no effect, or required access to configure the Apache server globally, but one of them inspired the solution I have now put in the update of the question. – njlarsson Mar 27 '22 at 16:36
  • Because you wrote "_Safari has the same problem as Chrome ... Firefox_" - those are clients. But they don't have problems. How does defining all filename extensions at once work out (`AddCharset UTF-8 .js .html .java .c .css`)? – AmigoJack Mar 27 '22 at 19:58
  • They have the problem of not displaying the characters correctly. If they defaulted to UTF-8 (which would be the sensible thing to do if they didn't have to worry about legacy), I would have been fine. But it should be clear from my question that what I was after was fixing the problem server-side. Adding the charsets on the same line makes no difference, I've only been able to fix this with `Files` or `FilesMatch` elements. – njlarsson Mar 28 '22 at 11:18