如何将 default.html 的字符编码设置为 UTF-8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/905173/
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 do I set Character Encoding to UTF-8 for default.html?
提问by Jacob de Lacey
I spent the last few hours getting my website to validate HTML 4.01 Strict and I actually have succeeded in that but there is still one warning which I can't get rid of. The warning is:
我花了最后几个小时让我的网站验证 HTML 4.01 Strict,实际上我已经成功了,但仍然有一个我无法摆脱的警告。警告是:
Character Encoding mismatch!
The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8). I will use the value from the HTTP header (iso-8859-1) for this validation.
字符编码不匹配!
HTTP 标头 (iso-8859-1) 中指定的字符编码与元素中的值 (utf-8) 不同。我将使用 HTTP 标头 (iso-8859-1) 中的值进行此验证。
The page in question is www.dubiousarray.net/default.html. As you can see from the page source I have the following meta element:
有问题的页面是www.dubiousarray.net/default.html。正如您从页面源代码中看到的,我有以下元元素:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
and I have made sure that the default.html file is saved with UTF-8 encoding. The strange thing is all the other pages in the site validate without this warning and they have the same meta tag and were saved in exactly the same way. I am pretty sure it is something to do with the server config. The .htaccess file looks like this at the moment:
并且我确保 default.html 文件使用 UTF-8 编码保存。奇怪的是,站点中的所有其他页面都在没有此警告的情况下进行验证,并且它们具有相同的元标记并且以完全相同的方式保存。我很确定这与服务器配置有关。.htaccess 文件现在看起来像这样:
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AddDefaultCharset UTF-8
but I have tried all the fixes shown on this pageand none of them worked. Does anyone have any ideas on how to go about getting rid of this warning? One last thing, in Firefox if you right click on the page and select 'View Page Info', default.html shows as ISO-8859-1 while all the other pages show UTF-8.
但我已经尝试了此页面上显示的所有修复程序,但没有一个奏效。有没有人对如何摆脱这个警告有任何想法?最后一件事,在 Firefox 中,如果您右键单击页面并选择“查看页面信息”,则 default.html 显示为 ISO-8859-1,而所有其他页面显示为 UTF-8。
Thanks, Jacob
谢谢,雅各布
EDIT: Something else to note is that all the html file have been created and saved in the exact same way (character encoding set to UTF-8 without BOM) but default.html is the only one which isn't displaying as UTF-8. So I assume the server is doing something special to the default.html file though I am not sure what as there is not sign of it in the .htaccess file.
编辑:还有一点需要注意的是,所有的 html 文件都以完全相同的方式创建和保存(字符编码设置为 UTF-8 没有 BOM)但 default.html 是唯一一个不显示为 UTF-8 . 所以我假设服务器正在对 default.html 文件做一些特殊的事情,尽管我不确定是什么,因为 .htaccess 文件中没有它的迹象。
回答by Larry K
You need to replace the HTTP-level header.
您需要替换 HTTP 级别的标头。
This should work:
这应该有效:
<?php
header('Content-type: text/html; charset=utf-8');
?>
Note that the above must be the FIRST thing in your file. No exceptions. See http://us2.php.net/manual/en/function.header.php
请注意,上述内容必须是您文件中的第一件事。没有例外。见 http://us2.php.net/manual/en/function.header.php
For general info on how to change the character set header in different web stacks, see http://www.w3.org/International/O-HTTP-charset
有关如何更改不同网络堆栈中的字符集标头的一般信息,请参阅 http://www.w3.org/International/O-HTTP-charset
Larry
拉里
回答by hannson
The server is clearly marking the document as ISO-8859-1 in the HTTP headers. Try saving default.html using UTF-8 encoding with a proper editor.
服务器在 HTTP 标头中清楚地将文档标记为 ISO-8859-1。尝试使用合适的编辑器使用 UTF-8 编码保存 default.html。
回答by Jacob de Lacey
Okay, I have come up with a partial solution to my problem. As it was only the default.html file which was causing the warning I assumed that the server was doing something special to it because of its name. So I made a new file called home.html with the same contents as the default.html file and pointed the .htaccess file to the new file (see line 3 below).
好的,我已经为我的问题提出了部分解决方案。由于只是 default.html 文件导致了警告,我认为服务器因其名称而对其进行了特殊处理。所以我创建了一个名为 home.html 的新文件,其内容与 default.html 文件相同,并将 .htaccess 文件指向新文件(参见下面的第 3 行)。
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
DirectoryIndex home.html
AddDefaultCharset UTF-8
This fixed the problem and all files are now recognised as UTF-8. I'm still not sure what the server was doing to the default.html file or where the settings concerning that are but as my problem is gone I will forget about that.
这解决了问题,现在所有文件都被识别为 UTF-8。我仍然不确定服务器对 default.html 文件做了什么,或者有关的设置在哪里,但随着我的问题消失,我会忘记这一点。
回答by Alan Haggai Alavi
Can you remove AddDefaultCharset
from .htaccess
and check the encoding? I saved the HTML source of your webpage and opened it. Encoding was detected as UTF-8. However, on viewing the same webpage served by your webserver, the encoding is ISO-8859-1. That is why I suggest removal of the former redundant rule.
您可以删除AddDefaultCharset
从.htaccess
检查编码?我保存了您网页的 HTML 源代码并打开了它。编码被检测为 UTF-8。但是,在查看由您的网络服务器提供的相同网页时,编码为 ISO-8859-1。这就是为什么我建议删除以前的冗余规则。
回答by Charuvind Atre
include this in your code:
将此包含在您的代码中:
<meta charset="utf-8" />