How to change the default encoding to UTF-8 for Ap

2018-12-31 20:24发布

I am using a hosting company and it will list the files in a directory if the file index.html is not there ,it uses iso-8859-1 as the default encoding.

If the server is Apache, is there a way to set UTF-8 as the default instead?

Update: Additionally I found out 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?

11条回答
梦该遗忘
2楼-- · 2018-12-31 20:38

For completeness, on Apache2 on Ubuntu, you will find the default charset in charset.conf in conf-available.

Uncomment the line

AddDefaultCharset UTF-8
查看更多
只靠听说
3楼-- · 2018-12-31 20:40

Place AddDefaultCharset UTF-8 into /etc/apache2/conf.d/charset. In fact, it's already there. You just have to uncomment it by removing the preceding #.

查看更多
刘海飞了
4楼-- · 2018-12-31 20:41

Where all the HTML files are in UTF-8 and don't have meta tags for content type, I was only able to set the needed default for these files to be sent by Apache 2.4 by adding both directives:

AddLanguage ru .html
AddCharset UTF-8 .html
查看更多
荒废的爱情
5楼-- · 2018-12-31 20:42

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#affffdefaultcharset).

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

查看更多
唯独是你
6楼-- · 2018-12-31 20:43

In .htaccess add this line:

AddCharset utf-8 .html .css .php .txt .js

This is for those that do not have access to their server's conf file. It is just one more thing to try when other attempts failed.

As far as performance issues regarding the use of .htaccess I have not seen this. My typical page load times are 150-200 mS with or without .htaccess

What good is performance if your page does not render correctly. Most shared servers do not allow user access to the config file which is the preferred place to add a character set.

查看更多
永恒的永恒
7楼-- · 2018-12-31 20:50

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.

查看更多
登录 后发表回答