如何将Apache的默认编码更改为UTF-8?

How to change the default encoding to UTF-8 for Apache?

我使用的是一家托管公司,如果文件index.html不在目录中,它将列出目录中的文件,并使用iso-8859-1作为默认编码。

如果服务器是Apache,是否有方法将utf-8设置为默认值?

更新:另外,我发现它实际上使用的是HTML3.2的doctype,而且根本没有字符集…所以它没有设置任何编码。但是有没有办法把它改成使用UTF-8?


在httpd.conf中添加(如果已经存在则更改):

1
AddDefaultCharset utf-8


添加到您的.htaccess中:

1
IndexOptions +Charset=UTF-8

或者,如果您有管理员权限,可以通过编辑httpd.conf并添加以下内容进行全局设置:

1
AddDefaultCharset utf-8

(您也可以在.htaccess中使用AddDefaultCharset,但不会以这种方式影响Apache生成的目录列表。)


请参阅adddefaultcharset指令、addcharset指令和本文。

1
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:

1
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.


AddDefaultCharset UTF-8放入/etc/apache2/conf.d/charset中。事实上,它已经存在了。您只需删除前面的#就可以取消对它的注释。


在.htaccess中添加此行:

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

这是为那些没有权限访问他们服务器的conf文件的人准备的。当其他尝试失败时,只需再尝试一件事。

关于使用.htaccess的性能问题,我没有看到这一点。我的典型页面加载时间是150-200 ms,有或没有.htaccess

如果页面呈现不正确,那么性能有多好。大多数共享服务器不允许用户访问配置文件,该文件是添加字符集的首选位置。


我不确定您是否可以访问apache配置(httpd.conf),但是您应该能够设置adddefaultcharset指令。见:

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

查找mod_mime.c模块并确保设置了以下内容:

1
AddDefaultCharset utf-8

或等效的apache 1.x文档(http://httpd.apache.org/docs/1.3/mod/core.html adddefaultcharset)。

但是,这仅在"响应内容类型为文本/纯文本或文本/HTML"时才有效。

您还应该确保您的页面也有一个字符集。更多信息请参见:

网址:http://www.w3.org/tr/rec-html40/charset.html


为了完整起见,在Ubuntu上的apache2上,可以在conf中的charset.conf中找到默认的charset。

取消对行的注释

1
AddDefaultCharset utf-8

这是未经测试的,但可能会起作用。

在.htaccess文件中输入:

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

但是,这将需要服务器上的mod_头文件。


如果您的文件名是长的UTF-8格式,那么这只是一个提示:默认情况下,它们将被缩短为20个字节,因此最后一个字符可能会被"切成两半",因此无法正确识别。然后您可能需要设置以下内容:

IndexOptions Charset=UTF-8 NameWidth=*

NameWidth设置将阻止缩短文件名,使其正确显示和可读。

正如前面提到的其他用户,这应该添加到httpd.confapache2.conf(如果您有管理权限)或.htaccess(如果您没有)。


如果所有HTML文件都是UTF-8格式,并且没有内容类型的元标记,那么我只能通过添加这两个指令来设置Apache2.4发送这些文件所需的默认值:

1
2
AddLanguage ru .html
AddCharset UTF-8 .html

空着就行了:'default_charset' in WHM :::::: default_charset =''

P.S.-为什么要回家?服务配置?php配置编辑器----)单击"高级模式"---查找"默认值"并将其留空----什么都不做,不是utf8,不是iso