How to change the default encoding to UTF-8 for Apache
Asked Answered
S

13

153

I am using a hosting company and it will list the files in a directory if the file index.html is not there. It uses ISO 8859-1 as the default encoding.

If the server is Apache, is there a way to set UTF-8 as the default instead?

I found out that it is actually using a DOCTYPE of HTML 3.2 and then there is not charset at all... so it is not setting any encoding. But is there a way to change it to use UTF-8?

Sainted answered 27/5, 2009 at 4:4 Comment(1)
This question is very old but currently (in 2021), at least in my case (Debian 10), the utf-8 characters are served properly and it seems that it's not needed to uncomment or change the setting AddDefaultCharset to utf-8 at all (On Debian, it's in /etc/apache2/conf-available/charset.conf).Lyns
F
194

In httpd.conf add (or change if it's already there):

AddDefaultCharset utf-8
Fanchie answered 27/5, 2009 at 4:11 Comment(8)
where in the file does one add this, anywhere?Seguidilla
@Seguidilla yes. if it's not already there, you can put it anywhere. however, i usually put every "custom" directive at the bottom of the file for a number of reasons (overriding pre-existing directives, order, and just to easily see what I did change from stock config).Fanchie
Add AddDefaultCharset utf-8 to .htaccess - worked a charm for me. (if you don't have access to httpd.conf)Zebe
Is it case sensitive?Ameliorate
Does this issue think is relevant to mine? #52577413 I did try to add this directive to httpd.conf but the problem remains.Winniewinnifred
Since this answer is from 2009: in Ubuntu 18, you change this configuration in /etc/apache2/conf-available/charset.confSunshinesunspot
You can also add it to apache2.conf.Cotangent
Note that this is the default in most GNU/Linux distributions.Weigel
B
63

Add this to your .htaccess:

IndexOptions +Charset=UTF-8

Or, if you have administrator rights, you could set it globally by editing httpd.conf and adding:

AddDefaultCharset UTF-8

(You can use AddDefaultCharset in .htaccess too, but it won’t affect Apache-generated directory listings that way.)

Boccherini answered 7/6, 2012 at 6:44 Comment(8)
This is a great solution and less invasive than modifying the httpd.conf file.Abdication
on my server, the .htaccess can affect all the subdirectories as well, probably apache will look for any .htaccess up the parent directory all the way to the root directory of the website folderSainted
Yes, that’s how .htaccess works on all servers — it affects all subdirectories as well. However, Apache-generated directory listing pages can’t be forced to UTF-8 by using .htaccess (AFAIK).Boccherini
Please note changing serverwide settings via .htaccess files is generally bad practice. Bugs become harder to track when server settings are distributed across various files. There's a slight performance hit too: with each requested file, Apache has to read the directory's .htaccess file and all .htaccess files of parent directories. .htaccess should therefore only be used for either directory specific settings (e.g. preventing access to a specific directory) or when there is absolutely no possibility to gain administrator rights.Security
Up voted, the IndexOptions +Charset=UTF-8 did the trick for me, thanks!Spit
on debian you want to change apache2.conf rather than httpd.conf source: control-escape.com/web/configuring-apache2-debian.htmlAbvolt
With Options -Indexes that I keep on for security reasons on Apache, the IndexOptions +Charset=UTF-8 does not work even for single filesBoson
The other answer didn't have a plus: IndexOptions Charset=UTF-8. I wonder which one(s) are right.Vermont
D
31

See AddDefaultCharset Directive, AddCharset Directive, and this article.

AddDefaultCharset utf-8

But I have to use Chinese characters now and then. Previously, I translated Chinese characters to Unicode code and include it in the document using the &# hack. But it is only useful for page having a few characters.

There is a better way to do that: encode the charset information in the filename, and apache will output the proper encoding header based on that. This is possible thanks to the AddCharset lines in the conf file, such as the line below:

conf/httpd.conf:

AddCharset UTF-8 .utf8

So if you have a file whose names ends in .html.utf8, apache will serve the page as if it is encoded in UTF-8 and will dump the proper character-encoding directive in the header accordingly.

Dibbell answered 27/5, 2009 at 4:11 Comment(0)
B
29

In file .htaccess, add this line:

AddCharset utf-8 .html .css .php .txt .js

This is for those that do not have access to their server's configuration file. It is just one more thing to try when other attempts failed.

As far as performance issues regarding the use of file .htaccess, I have not seen this. My typical page load times are 150-200 ms with or without file .htaccess.

What good is performance if your page does not render correctly? Most shared servers do not allow user access to the configuration file which is the preferred place to add a character set.

Brim answered 12/8, 2014 at 3:9 Comment(7)
I can't explain, but only this solution works for me. That's why a big +1Semiramis
As mentioned by @Security earlier - if you are not already using .htaccess files, don't start now. There are performance & administrative reasons why this is a Bad Idea(tm)Titian
When you do not include the extensions AddCharset is applied to Content Types text/html and text/plain.Brim
This worked for me while all the above awnsers didnt. +1Russelrussell
The accepted answer only affects tex/html and text/plain: httpd.apache.org/docs/2.4/mod/core.html#adddefaultcharsetSubmerged
What about for files, with no extension, e.g., Makefile. Do I use AddCharset utf-8 ""?Vermont
OK, elevating to #66775024Vermont
F
25

On Ubuntu 12.04, it's sufficient to uncomment the line AddDefaultCharset UTF-8 in /etc/apache2/conf.d/charset. If you're using upstream Apache, the file may be called httpd.conf, and you may have to insert the line.

Frazzled answered 6/3, 2013 at 16:49 Comment(3)
There is no such file as /etc/apache2/conf.d/charset. It is a custom include file by your distribution. As is any other file that’s not httpd.conf.Griffith
Its /etc/apache2/conf-enabled/charset.conf on my distribution(Ubuntu 16.4).Also didnt work.Russelrussell
Can you update your answer, e.g. with Linux distribution information, incl. version. E.g., what was the original Linux distribution and version? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)Elementary
M
12

This is untested, but it will probably work.

In your .htaccess file, add:

<Files ~ "\.html?$">  
     Header set Content-Type "text/html; charset=utf-8"
</Files>

However, this will require mod_headers on the server.

Moquette answered 27/5, 2009 at 4:9 Comment(4)
That worked for me, whereas the chosen solution did not. Thank you! In fact, I didn't even have to wrap it in <Files> tags.Vulgate
What is "mod_headers"? Where does it go or how is it set?Elementary
This worked for me, when none of the other answers would. I also found out that there is a FilesMatch tag that also worked.Bluegreen
Same here. I spent hours with this, finally this solution worked. All the AddDefaultCharset approaches would not. This is with Apache 2.4.57 on FreeBSD 13.3.Stipendiary
F
12

For completeness, on Apache2 on Ubuntu, you will find the default charset in charset.conf in conf-available.

Uncomment the line

AddDefaultCharset UTF-8
Fermat answered 26/8, 2014 at 0:27 Comment(2)
What is "conf-available"? A section in a configuration fille? A file? Where is the file located?Elementary
On Ubunto 20.04 the file is here: /etc/apache2/conf-available/charset.confLeer
G
11

I'm not sure whether you have access to the Apache config (httpd.conf) but you should be able to set an AddDefaultCharset Directive. See:

http://httpd.apache.org/docs/2.0/mod/core.html

Look for the mod_mime.c module and make sure the following is set:

AddDefaultCharset utf-8 

or the equivalent Apache 1.x docs (http://httpd.apache.org/docs/1.3/mod/core.html#adddefaultcharset).

However, this only works when "the response content-type is text/plain or text/html".

You should also make sure that your pages have a charset set as well. See this for more info:

http://www.w3.org/TR/REC-html40/charset.html

Glidewell answered 27/5, 2009 at 4:12 Comment(0)
S
5

Just a hint if you have long filenames in UTF-8 format: by default they will be shortened to 20 bytes, so it may happen that the last character might be "cut in half" and therefore unrecognized properly. Then you may want to set the following:

IndexOptions Charset=UTF-8 NameWidth=*

NameWidth setting will prevent shortening your file names, making them properly displayed and readable.

As other users already mentioned, this should be added either in httpd.conf or apache2.conf (if you do have admin rights) or in .htaccess (if you don't).

Saddletree answered 3/5, 2016 at 20:40 Comment(2)
What shorten them to 20 bytes? What is the context?Elementary
Say if we should use the plus: IndexOptions +Charset=UTF-8 NameWidth=* . The other solution had the plus.Vermont
W
3

Where all the HTML files are in UTF-8 and don't have meta tags for content type, I was only able to set the needed default for these files to be sent by Apache 2.4 by adding both directives:

AddLanguage ru .html
AddCharset UTF-8 .html
Witwatersrand answered 11/12, 2014 at 7:59 Comment(0)
U
3

Just leave it empty: 'default_charset' in WHM :::::: default_charset =''

P.S.: In WHM, go → HomeService ConfigurationPHP Configuration Editor → click 'Advanced Mode' → find 'default_charset' and leave it blank. Just nothing, not UTF-8 and not ISO.

Ursas answered 4/11, 2016 at 9:55 Comment(1)
ISO what? ISO 8859-1?Elementary
A
1

<meta charset='utf-8'> overrides the Apache default charset (cf /etc/apache2/conf.d/charset)

If this is not enough, then you probably created your original file with the ISO 8859-1 encoding character set. You have to convert it to the proper character set:

iconv -f ISO-8859-1 -t UTF-8 source_file.php -o new file.php
Alkalize answered 22/10, 2020 at 16:37 Comment(0)
C
0

In my case I added this to file .htaccess:

AddDefaultCharset off
AddDefaultCharset windows-1252
Cruet answered 11/1, 2020 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.