Dejan Levec

Setting Charset on an Apache Server

[b]How to demand from an Apache Server to serve pages with a specific encoding?[/b]

You will ask this question sooner or later if you are making web sites. Probably sooner if you are dealing with non-ASCII characters.

There are a lot of charsets in the world, from ones that support most languages to ones that are very specific. In web development business UTF-8 is a standard, specially on non-english web sites. It\’s not that big of a problem on HTML sites, but it can be a hell of a mess if you have server with first encoding, PHP with second encoding and MySQL with completely different encoding.

In this post, I will show you how to tell Apache to use specific charset (in our examples I\’ll use UTF-8).

The easiest way to set encoding is by following meta tag in HTML:
[quote]<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″>[/quote]
But this is not always enough. So to be sure you need to specify charset in the Apache configuration file or in .htaccess.

You can use one of the following methods:
[quote]<FilesMatch “\\.(htm|html|css|js|php)$”>
ForceType \’text/html; charset=UTF-8\’
</FilesMatch>[/quote]
or
[quote]AddType \’text/html; charset=UTF-8\’ html[/quote]
or
[quote]AddCharset UTF-8 .html[/quote]
or
[quote]AddDefaultCharset UTF-8[/quote]
I recommend last option because I had no problems what so ever with it so far. It always works fine. (Other should too.)

Leave a Reply

Your email address will not be published. Required fields are marked *