【问题标题】:How to specify the character set in the HTTP Content-Type response header?如何在 HTTP Content-Type 响应头中指定字符集?
【发布时间】:2011-12-19 06:54:57
【问题描述】:

我用 Google 的 Page Speed 应用程序测试了我的网站,其中一个建议是在 HTTP Content-Type 响应标头中指定字符集,声称它比仅在元标记中更好。

以下是我理解我需要写的内容: 内容类型:文本/html; charset=UTF-8

..但是我应该把这个放在哪里?我在共享服务器上。

谢谢!

【问题讨论】:

    标签: optimization character-encoding http-headers content-type


    【解决方案1】:

    当我添加这个时,我的响应头看起来像这样:

    HTTP/1.1 200 OK
    内容类型:text/html,text/html;charset='UTF-8'
    变化:接受编码
    服务器:Microsoft-IIS/7.5

    【讨论】:

    【解决方案2】:

    Apache:添加到根目录下的 .htaccess 文件中:

    AddDefaultCharset UTF-8
    

    它将从此修改标题:

    内容类型文本/html

    ...到这个:

    内容类型文本/html; charset=UTF-8


    nginx [doc] [serverfault Q]

    server {
       # other server config...
       charset utf-8;
    }
    

    charset utf-8; 添加到 server 块(并重新加载 nginx 配置)

    【讨论】:

    • 我知道它应该可以正常工作,但我的 HTTP 标头仍然只是响应 Content-Type text/html。我不想在文档中使用 标记来声明字符编码。我使用 Apache 2.4。
    • @Tomkay 确保服务器进程 .htaccess 指令并且文件位于正确的目录中
    【解决方案3】:

    使用 Apache,您可以使用 http://httpd.apache.org/docs/2.2/mod/core.html#adddefaultcharset

    使用 IIS,您可以在文件列表中编辑文件类型的 MIME 类型。

    对于大多数服务器端技术,如 PHP 或 ASP.NET,该技术提供了一种方法或属性。例如,在 ASP.NET 中,您可以在 config、page 或 page code-behind 中设置它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-13
      • 2019-04-04
      • 2015-04-17
      • 2022-06-25
      • 2013-01-20
      • 2014-04-25
      • 2015-11-26
      • 1970-01-01
      相关资源
      最近更新 更多