Apache2 unseting AddDefaultCharset (via .htaccess)

Setting a default charset using .htaccess is a simple thing to do

However sometimes there is a need to unset default charset as well (for instance if the AddDefaultCharset is set in the apache config and this is causing problems).

This is very easy to do just edit the .htaccess file and add a line that says

AddDefaultCharset OFF

Linux: Converting a file encoded in ISO-8859-1 to UTF-8

If you have a file that is saves as ISO-8859-1 (or ISO-LATIN-1 if you like to call it that) and wish to convert it to UTF-8 you can use:

 iconv --from-code=ISO-8859-1 --to-code=UTF-8 ./oldfile.htm > ./newfile.html

This will create a new file with the converted encoding.

iconv can of of course convert to and from several other charsets. To see a list of all the encodings that iconv can work with use:

 iconv -l

If you wish to massconvert files find can be used with exec

 find . -name "*.txt" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o {}.utf8 \;

Apache2 changing charset using .htaccess

If you do not wish to change character encoding for the entire server, but only one site (or only one directory) then this is possible to do using .htaccess.

AddDefaultCharset UTF-8

If you only wish to do this to php or htm files (and not all files)

<FilesMatch "\.(htm|php)$">
AddDefaultCharset UTF-8

If you wish to modify the mime-type as well as the encoding (on html files in this example)

AddType 'text/html; charset=UTF-8' html