1

I put up some source code files for viewing on the web. The problem is, browsers tend to not understand that these files are in UTF-8, and I wonder if there's anything I can do about that. (I'm sure some browsers use UTF-8 per default, but Safari at least doesn't. I have to go into the View menu and choose the encoding manually to make it look right.)

My set of webpages is here, with several Python files and one C file: fgcode.avadeaux.net. I tried putting a charset="UTF-8" attribute in the a elements in the , but that doesn't seem to help. The Python files declare the charset in a comment on the top line, but the browser ignores that as well (which is no surprise, it's not a Python source code browser after all).

I don't have much control over the web server, it's run by my ISP and I just put the files there. But I'd still be interested to know if this problem has a web server configuration solution.

Solution: solved, as per links provided, by adding an .htaccess file with the following content:

AddDefaultCharset UTF-8
AddCharset UTF-8 .py
AddCharset UTF-8 .c

Only the first line wasn't enough in my case.

njlarsson
  • 2,128
  • 1
  • 18
  • 27
  • 1
    This is not a html question, and should not be closed as a duplicate... It's about content delivery headers for non-html files... – Slawomir Chodnicki May 30 '19 at 14:49
  • @Slawomir Arguably, but the [duplicate answer](https://stackoverflow.com/a/423752/476) lays it out quite perfectly. – deceze May 30 '19 at 14:51
  • @deceze♦ As far as I can see, the “duplicate answer” answers a different question (which I already knew the answer for). The links in @Slawomir's answer are more relevant. – njlarsson May 30 '19 at 15:02

2 Answers2

1

If you are allowed to place a .htaccess file in your content directory, you might be able to specify a charset there.

YMMV depending on which server software they run, and how permissive the config is.

See:

How to change the default encoding to UTF-8 for Apache?

htaccess UTF-8 encoding for .html, .css, .js - Whats the best way?

Slawomir Chodnicki
  • 1,525
  • 11
  • 16
-1

You need a <head> element in the document being served. In <head> you will also need: <meta charset="utf-8">.

If the document you're linking to does not have this, you may need to set up a proxy to wrap it. Browsers understand HTML. Your .py files are just text, so they contain not instructions the browser will understand.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176