简体   繁体   中英

Wrong charset in HTTP Content-Type response

I have a japanese RHEL server running apache 2.0.

And this is my problem:
I want response with 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=Shift_JIS

What I've tried:

  • Add AddDefaultCharset utf-8 to working .htaccess file (Can not edit apache config file)
  • Add <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> to php file
  • Add php_value default_charset UTF-8 to .htaccess file
  • Test with very simple 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. If there's another directive that sets a different Content-Type for your file type, your setting will be overridden.

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.

Please check the default_charset PHP directive for the details:

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() .

Obviously, anything you put in the response body (such as <meta> tags) does not alter the response headers sent earlier.

Figured out that the problem was related to mbstring settings in php.ini. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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