简体   繁体   English

HTTP Content-Type响应中的错误字符集

[英]Wrong charset in HTTP Content-Type response

I have a japanese RHEL server running apache 2.0. 我有一个运行apache 2.0的日语RHEL服务器。

And this is my problem: 这是我的问题:
I want response with Content-Type: text/html; charset=UTF-8 我想要Content-Type: text/html; charset=UTF-8 Content-Type: text/html; charset=UTF-8 but currently it responses with Content-Type: text/html; charset=Shift_JIS Content-Type: text/html; charset=UTF-8但目前它使用Content-Type: text/html; charset=Shift_JIS响应Content-Type: text/html; charset=Shift_JIS Content-Type: text/html; charset=Shift_JIS

What I've tried: 我尝试过的:

  • Add AddDefaultCharset utf-8 to working .htaccess file (Can not edit apache config file) AddDefaultCharset utf-8添加到工作.htaccess文件(无法编辑apache配置文件)
  • Add <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> to php file <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />到php文件
  • Add php_value default_charset UTF-8 to .htaccess file php_value default_charset UTF-8添加到.htaccess文件
  • Test with very simple test.php : 用非常简单的test.php测试:

    <?php header('Content-Type: text/html; charset=UTF-8'); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h1>Test</h1> </body> </html>

None of above works. 以上都不适用。 Someone please help me. 有人请帮帮我。

As the name suggests, AddDefaultCharset changes the default charset. 顾名思义, AddDefaultCharset更改默认字符集。 If there's another directive that sets a different Content-Type for your file type, your setting will be overridden. 如果有另一个指令为您的文件类型设置不同的Content-Type,则将覆盖您的设置。

In this case, PHP itself will always set the Content-Type header if you don't do it yourself so there's no way to configure it from Apache unless you run PHP as Apache module and use the php_... Apache directives. 在这种情况下,如果你自己不这样做,PHP本身将始终设置Content-Type标头,因此除非你将PHP作为Apache模块运行并使用php_... Apache指令,否则无法从Apache配置它。

Please check the default_charset PHP directive for the details: 请查看default_charset PHP指令以获取详细信息:

All versions of PHP will use this value as the charset within the default Content-Type header sent by PHP if the header isn't overridden by a call to header() . 如果标头未被header()调用覆盖,则所有版本的PHP都将使用此值作为PHP发送的默认Content-Type标头中的字符集。

Obviously, anything you put in the response body (such as <meta> tags) does not alter the response headers sent earlier. 显然,您放在响应主体中的任何内容(例如<meta>标记)都不会改变之前发送的响应标头

Figured out that the problem was related to mbstring settings in php.ini. 弄清楚问题与php.ini中的mbstring设置有关。 It works when changing from 它从改变时起作用

mbstring.language = Japanese
mbstring.http_input = auto
mbstring.http_output = SJIS

to

mbstring.language = Japanese
mbstring.http_input = pass
mbstring.http_output = pass

But at first, check Álvaro González's answer for this problem. 但首先,请查看ÁlvaroGonzález对此问题的回答

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

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