Share via:

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.

1255 views

Edited: 2018-02-05 13:11

How to change the default characterset used by Apache will depend on your specific setup. This tutorial is focused on Debian & Ubuntu based configurations.

The easiest, and recommended way, is to add a custom .conf file. If you are hosting multiple domains on the same server, the individual v-host files is the place to go. 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 charset.conf, but this might not be desirable in a VirtualHost environment where multiple character sets is in use.

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, it 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, either add your own configuration files to /etc/apache2/conf-available/ and enable them with the a2enconf command, or edit the existing files.

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

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

Comments