如何将 Apache 的默认编码更改为 UTF-8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/913869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the default encoding to UTF-8 for Apache?
提问by nonopolarity
I am using a hosting company and it will list the files in a directory if the file index.htmlis not there, it uses iso-8859-1 as the default encoding.
我正在使用托管公司,如果文件index.html不存在,它将列出目录中的文件,它使用 iso-8859-1 作为默认编码。
If the server is Apache, is there a way to set UTF-8 as the default instead?
如果服务器是 Apache,有没有办法将 UTF-8 设置为默认值?
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?
更新:另外我发现它实际上使用的是 HTML 3.2 的 DOCTYPE,然后根本没有字符集......所以它没有设置任何编码。但是有没有办法将其更改为使用 UTF-8?
回答by MartinodF
In httpd.conf add (or change if it's already there):
在 httpd.conf 添加(或更改,如果它已经存在):
AddDefaultCharset utf-8
回答by Mathias Bynens
Add this to your .htaccess:
将此添加到您的.htaccess:
IndexOptions +Charset=UTF-8
Or, if you have administrator rights, you could set it globally by editing httpd.confand adding:
或者,如果您有管理员权限,您可以通过编辑httpd.conf和添加来全局设置:
AddDefaultCharset UTF-8
(You can use AddDefaultCharsetin .htaccesstoo, but it won't affect Apache-generated directory listings that way.)
(您也可以使用AddDefaultCharsetin .htaccess,但它不会以这种方式影响 Apache 生成的目录列表。)
回答by Eugene Yokota
See AddDefaultCharset Directive, AddCharset Directive, and this article.
请参阅AddDefaultCharset 指令、AddCharset 指令和本文。
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
AddCharsetlines in the conf file, such as the line below:
conf/httpd.conf:
但我不得不时不时地使用汉字。以前,我使用
&#hack将中文字符翻译成 Unicode 代码并将其包含在文档中 。但它只对有几个字符的页面有用。有一个更好的方法来做到这一点:对文件名中的字符集信息进行编码,apache 将基于此输出正确的编码头。这要归功于
AddCharsetconf 文件中的行,例如以下行:
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.
因此,如果您有一个名称以
.html.utf8.
回答by Bjartur Thorlacius
Place AddDefaultCharset UTF-8into /etc/apache2/conf.d/charset. In fact, it's already there. You just have to uncomment it by removing the preceding #.
地方AddDefaultCharset UTF-8到/etc/apache2/conf.d/charset。事实上,它已经在那里了。您只需要通过删除前面的#.
回答by Misunderstood
In .htaccess add this line:
在 .htaccess 中添加这一行:
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.
这适用于那些无权访问其服务器的 conf 文件的人。当其他尝试失败时,再尝试一件事。
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
至于有关使用 .htaccess 的性能问题,我还没有看到这一点。我的典型页面加载时间是 150-200 毫秒,有或没有 .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.
如果您的页面无法正确呈现,性能有什么好处。大多数共享服务器不允许用户访问配置文件,这是添加字符集的首选位置。
回答by Jon
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:
我不确定您是否有权访问 Apache 配置 (httpd.conf),但您应该能够设置 AddDefaultCharset 指令。看:
http://httpd.apache.org/docs/2.0/mod/core.html
http://httpd.apache.org/docs/2.0/mod/core.html
Look for the mod_mime.c module and make sure the following is set:
查找 mod_mime.c 模块并确保设置以下内容:
AddDefaultCharset utf-8
or the equivalent Apache 1.x docs (http://httpd.apache.org/docs/1.3/mod/core.html#adddefaultcharset).
或等效的 Apache 1.x 文档(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".
但是,这仅在“响应内容类型为 text/plain 或 text/html”时有效。
You should also make sure that your pages have a charset set as well. See this for more info:
您还应该确保您的页面也有一个字符集。请参阅此了解更多信息:
回答by MiffTheFox
This is untested but will probably work.
这是未经测试的,但可能会起作用。
In your .htaccess file put:
在您的 .htaccess 文件中:
<Files ~ "\.html?$">
Header set Content-Type "text/html; charset=utf-8"
</Files>
However, this will require mod_headers on the server.
但是,这将需要服务器上的 mod_headers。
回答by David Glance
For completeness, on Apache2 on Ubuntu, you will find the default charset in charset.conf in conf-available.
为了完整起见,在 Ubuntu 上的 Apache2 上,您将在 conf-available 的 charset.conf 中找到默认字符集。
Uncomment the line
取消注释该行
AddDefaultCharset UTF-8
回答by Alex
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:
在所有 HTML 文件都在 UTF-8 中并且没有内容类型的元标记的情况下,我只能通过添加两个指令为 Apache 2.4 发送的这些文件设置所需的默认值:
AddLanguage ru .html
AddCharset UTF-8 .html
回答by pstryk
Just a hint if you have long filenames in utf-8: by default they will be shortened to 20 bytes, so it may happen that the last character might be "cut in half" and therefore unrecognized properly. Then you may want to set the following:
如果您有 utf-8 格式的长文件名,请提示一下:默认情况下,它们将被缩短为 20 个字节,因此可能会发生最后一个字符可能被“切成两半”而无法正确识别的情况。那么您可能需要设置以下内容:
IndexOptions Charset=UTF-8 NameWidth=*
IndexOptions Charset=UTF-8 NameWidth=*
NameWidthsetting will prevent shortening your file names, making them properly displayed and readable.
NameWidth设置将防止缩短您的文件名,使它们正确显示和可读。
As other users already mentioned, this should be added either in httpd.confor apache2.conf(if you do have admin rights) or in .htaccess(if you don't).
正如其他用户已经提到的,这应该添加到httpd.conf或apache2.conf(如果您有管理员权限)或.htaccess(如果您没有)。

