简体   繁体   English

如何为default.html将字符编码设置为UTF-8?

[英]How do I set Character Encoding to UTF-8 for default.html?

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. 我花了最后几个小时让我的网站验证HTML 4.01严格,我实际上已经成功,但仍然有一个我无法摆脱的警告。 The warning is: 警告是:

Character Encoding mismatch! 字符编码不匹配!

The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8). HTTP标头(iso-8859-1)中指定的字符编码与元素(utf-8)中的值不同。 I will use the value from the HTTP header (iso-8859-1) for this validation. 我将使用HTTP头(iso-8859-1)中的值进行此验证。

The page in question is www.dubiousarray.net/default.html . 有问题的页面是www.dubiousarray.net/default.html As you can see from the page source I have the following meta element: 从页面源代码中我可以看到,我有以下元元素:

<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. 我确保default.html文件使用UTF-8编码保存。 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: .htaccess文件目前看起来像这样:

# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AddDefaultCharset UTF-8

but I have tried all the fixes shown on this page and 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. 编辑:需要注意的是,所有的html文件都是以完全相同的方式创建和保存的(字符编码设置为UTF-8而没有BOM)但default.html是唯一一个不显示为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. 所以我假设服务器正在对default.html文件做一些特殊的事情,虽然我不确定.htaccess文件中没有它的标志。

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 有关如何更改不同Web堆栈中的字符集标题的一般信息,请参阅http://www.w3.org/International/O-HTTP-charset

Larry 拉里

The server is clearly marking the document as ISO-8859-1 in the HTTP headers. 服务器在HTTP标头中清楚地将文档标记为ISO-8859-1。 Try saving default.html using UTF-8 encoding with a proper editor. 尝试使用正确的编辑器使用UTF-8编码保存default.html。

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. 因为它只是导致警告的default.html文件,所以我假设服务器因为它的名字而对它做了一些特殊的事情。 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). 所以我创建了一个名为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. 这解决了问题,现在所有文件都被识别为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. 我仍然不确定服务器对default.html文件做了什么,或者相关的设置在哪里但是因为我的问题已经消失,我将忘记这一点。

在您的代码中包含此内容:

 <meta charset="utf-8" /> 

Can you remove AddDefaultCharset from .htaccess and check the encoding? 你能从.htaccess删除AddDefaultCharset并检查编码吗? I saved the HTML source of your webpage and opened it. 我保存了您网页的HTML源代码并将其打开。 Encoding was detected as UTF-8. 编码被检测为UTF-8。 However, on viewing the same webpage served by your webserver, the encoding is ISO-8859-1. 但是,在查看由您的网络服务器提供的同一网页时,编码为ISO-8859-1。 That is why I suggest removal of the former redundant rule. 这就是为什么我建议删除以前的冗余规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM