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

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?

Update: Additionally 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?

This question is related to apache character-encoding apache-config

The answer is


This is untested but will probably work.

In your .htaccess file put:

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

However, this will require mod_headers on the server.


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

Uncomment the line

AddDefaultCharset UTF-8

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

In my case add to .htaccess

AddDefaultCharset off
AddDefaultCharset windows-1252

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


In .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 conf file. It is just one more thing to try when other attempts failed.

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

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


Just a hint if you have long filenames in utf-8: 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).


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.


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.)


Place AddDefaultCharset UTF-8 into /etc/apache2/conf.d/charset. In fact, it's already there. You just have to uncomment it by removing the preceding #.


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

p.s. - In WHM go --------) Home »Service Configuration »PHP Configuration Editor ----) click 'Advanced Mode' ----) find 'default_charset' and leave it blank ---- just nothing, not utf8, not ISO


<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 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

Examples related to apache

Enable PHP Apache2 Switch php versions on commandline ubuntu 16.04 Laravel: PDOException: could not find driver How to deploy a React App on Apache web server Apache POI error loading XSSFWorkbook class How to enable directory listing in apache web server Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details How to enable php7 module in apache? java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer

Examples related to character-encoding

Changing PowerShell's default output encoding to UTF-8 JsonParseException : Illegal unquoted character ((CTRL-CHAR, code 10) Change the encoding of a file in Visual Studio Code What is the difference between utf8mb4 and utf8 charsets in MySQL? How to open html file? All inclusive Charset to avoid "java.nio.charset.MalformedInputException: Input length = 1"? UTF-8 output from PowerShell ERROR 1115 (42000): Unknown character set: 'utf8mb4' "for line in..." results in UnicodeDecodeError: 'utf-8' codec can't decode byte How to make php display \t \n as tab and new line instead of characters

Examples related to apache-config

.htaccess not working apache Configure apache to listen on port other than 80 index.php not loading by default Best way to log POST data in Apache? how to use "AND", "OR" for RewriteCond on Apache? How to change the default encoding to UTF-8 for Apache?