Apache: Changing the Default Character Set

Changing the default charset for the Apache HTTP server may be done by changing either the v-host .conf files or the charset.conf file. If you do not have access to these files, you can try .htaccess instead. Find out how in this tutorial.

14315 views

Edited: 2020-11-26 07:27

How to change the default characterset used by Apache will depend on your specific setup; this tutorial concentrates on Debian & Ubuntu based configurations.

The easiest, and recommended, way to change the character set is to add a custom .conf file that you can include in your website configuration. For example, if you are hosting multiple domains on the same server, you could edit the individual v-host files; but it would be easier to have a custom .conf file with shared configurations that you can include. Usually you will find the v-host configuration files in /etc/apache2/sites-available. To change the charset for all domains on the server, you can edit /etc/apache2/conf-available/charset.conf — but this is not ideal in an environment where different character sets is in use for different VirtualHost's.

Changing the character set via the v-host .conf files can be done by adding AddDefaultCharset UTF-8 to the file. However, since some hosting providers prevent editing the server configuration files, this will not always possible. Alternatively, you can sometimes use .htaccess files instead. To do this, simply add IndexOptions +Charset=UTF-8 to a .htaccess file and place it in your websites root directory.

Note. You should not change the httpd.conf or apache2.conf files. Instead, you should add your own configuration files to /etc/apache2/conf-available/ and enable them with the a2enconf command, or edit the existing files in the directory. Note that configurations in these files will typically apply for all v-host's.

After changing the character set, make sure that you are not setting a different charset in your meta or XML encoding tags, since these may be overwritten by server-sided HTTP headers, such as those added with the AddDefaultCharset directive or with PHP's header function.

AddDefaultCharset will only change the charset for text/plain or text/html content-types.

Apache configuration files

If you have access to the Apache configuration files, then you can easily change default character set by editing either the individual v-host files, or the main configuration file, apache2.conf. The below is an example of a v-host .conf:

# ***********
# used for testing purposes
# ***********
<VirtualHost *:80>
    DocumentRoot "/var/www/testing/"
    ServerName testing.com
    ErrorLog ${APACHE_LOG_DIR}/testing.com-error.log
    CustomLog ${APACHE_LOG_DIR}/testing.com-access.log common
    <Directory "/var/www/testing/">
        Options Indexes FollowSymLinks
        AllowOverride All

        Order allow,deny
        Allow from all

        AddDefaultCharset utf-8
    </Directory>

</VirtualHost>

</pre>

The location for these files is usually: /etc/apache2/sites-available

To apply changes globally, you should either add your own .conf files to /etc/apache2/conf-available/, or change one of the existent files. To change charset, create a file (I.e. myconf.conf) add the following to the file:

AddDefaultCharset utf-8

Or simply uncomment the AddDefaultCharset directive in the charset.conf file (if it exists on your system).

# Read the documentation before enabling AddDefaultCharset.
# In general, it is only a good idea if you know that all your files
# have this encoding. It will override any encoding given in the files
# in meta http-equiv or xml encoding tags.

AddDefaultCharset UTF-8

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Note. httpd.conf is deprecated, and apache2.conf can be overwritten by the system – do not use these files to add user configurations!

Htaccess files

Some hosting providers do not allow you to change the configuration files directly, but they usually still allow the use of .htaccess files. You can use htaccess to change many of the same configuration options found in the main files. To change the default charset in htaccess, you can add the following to a .htaccess file: IndexOptions +Charset=UTF-8

An example .htaccess file can look like the below:

DirectoryIndex index.php index.html

IndexOptions +Charset=UTF-8

Adding a .htaccess file to your root directory would affect all subdirectories of your website. I.e:

  • example.com/
  • example.com/somedir/some-file.html
  • example.com/somedir/blah/another-file.html

Important information on character sets

When deciding on a character set for your web applications, it is not enough to change the character set in the content-type HTTP header; you should also make sure that the data itself is actually encoded using the specified character set; if working with source code or static HTML files, you may change the encoding in an editor like Visual Studio Code.

In addition, if you are using a database like MySQL, you should also make sure that the database, tables, and columns are using the correct character set and collation.

The character set is a set of symbols and encoding, while the collation defines rules for comparing characters in character sets. Understanding this, and getting this right may take some practice.

More recently, utf8mb4 has become a good starting point, since this allows storage of 4-byte characters where's the older utf8 only allows storage of 3-byte characters. This means, utf8mb4 has support for a wider set of characters.

Using a collation of utf8mb4_0900_ai_ci or utf8mb4_general_ci on the table-columns themselves is probably best, just remember to be consistent and also define the character set and collate for your database connections. Note that utf8mb4_0900_ai_ci is newer, and default in MySQL 8.0.1.

Tell us what you think:

David Stellar

Your "IndexOptions +Charset=UTF-8" solution only works for directory indexes, not for the files contained within. That requires AddCharset or ForceType

  1. Understanding file permissions in Unix / Linux based systems, and how to make files immutable.
  2. In this article I will explain how to enable a swapfile on small instances, and why it might be useful, even if you do have enough physical memory.
  3. How to determine an optimal value for pm.max_children and related php-fpm settings for your server and web applications.
  4. Tutorial showing how to configure a VirtualBox Guest VM with HOST-only and NAT adapter, while using the WWW folder from the HOST OS.
  5. You may have wondered what the /etc/php/8.0/conf.d/ directory is for in Debian and Ubuntu, and whether it is better to edit the conf.d files than editing php.ini directly; find out in this Tutorial.

More in: Linux servers