views:

1689

answers:

4

i am using a hosting company and it will list the files in a directory if an index.html is not there. However, it will use iso-8859-1 as the default encoding. if the server is apache, is there a way to change it to use UTF-8 as a default instead?

Update: actually, i found that it is actually using a DOCTYPE of HTML 3.2 and then there is not charset at all... so it is not setting any encoding. But is there a way to change it to use UTF-8?

A: 

This is untested but will probably work.

In your .htaccess file put:

<Files ~ "\.html?$">  
     Header set Content-Type text/html; charset=utf-8 
</Files>

However, this will require mod_headers on the server.

MiffTheFox
+2  A: 

See AddDefaultCharset Directive, AddCharset Directive, and this article.

AddDefaultCharset utf-8

But I have to use Chinese characters now and then. Previously, I translated Chinese characters to Unicode code and include it in the document using the &# hack. But it is only useful for page having a few characters.

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.

eed3si9n
+3  A: 

In httpd.conf add (or change if it's already there):

AddDefaultCharset utf-8
MartinodF
@MartinodF, where in the file does one add this, anywhere?
Geoffrey Van Wyk
@Geoffrey yes. if it's not already there, you can put it anywhere. however, i usually put every "custom" directive at the bottom of the file for a number of reasons (overriding pre-existing directives, order, and just to easily see what I did change from stock config).
MartinodF
+1  A: 

I'm not sure whether you have access to the Apache config (httpd.conf) but you should be able to set an AddDefaultCharset Directive. See:

http://httpd.apache.org/docs/2.0/mod/core.html

Look for the mod_mime.c module and make sure the following is set:

AddDefaultCharset utf-8

or the equivalent Apache 1.x docs (http://httpd.apache.org/docs/1.3/mod/core.html#adddefaultcharset).

However, this only works when "the response content-type is text/plain or text/html".

You should also make sure that your pages have a charset set as well. See this for more info:

http://www.w3.org/TR/REC-html40/charset.html

Jon