Developers Heaven Forum

Operating Systems => Lunix & Unix => Topic started by: admin on November 20, 2009, 07:25:31 PM

Title: Apache encoding
Post by: admin on November 20, 2009, 07:25:31 PM
Some hosting providers have problems with encodings. Default they serve HTML documents as ISO-8859-1 encoding (via the HTTP Content-Type header). That is so last century. a simple solution that works for me. Create a file named .htaccess (the beginning dot is necessary, and renders the file invisible on UNIX/Linux systems) with the following content:

Code: [Select]
AddDefaultCharset utf-8
AddCharset utf-8 .txt .html .htm .php
Title: Re: Apache encoding
Post by: admin on November 20, 2009, 07:27:19 PM
If you want to set header of php files without .htaccess you can use:

header(‘Content-Type: text/html; charset=utf-8′);
or
header(‘Content-Type: text/html; charset=iso-8859-1′);

Title: Re: Apache encoding
Post by: admin on November 20, 2009, 07:30:32 PM
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.