Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the character set in the HTTP Content-Type response header?

I had my site tested with the Page Speed app from Google and one of the suggestions was to specify the character set in the HTTP Content-Type response header claiming it was better than just in a meta tag.

Here's what I understand I need to write: Content-Type: text/html; charset=UTF-8

..but where exactly should I put this? I'm on a shared server.

Thank you!

like image 567
Cris Avatar asked Nov 01 '11 07:11

Cris


People also ask

How do I set character encoding in HTTP header?

html,... Then, for Content type, add " text/html;charset=utf-8 " (without the quotes; substitute your desired charset for utf-8; do not leave any spaces anywhere because IIS ignores all text after spaces). For IIS 4, you may have to use "HTTP Headers" => "Creating a Custom HTTP Header" if the above does not work.

How do you specify Content-Type in a URL?

To specify the content types of the request body and output, use the Content-Type and Accept headers. Indicates that the request body format is JSON. Indicates that the request body format is XML. Indicates that the request body is URL encoded.

What does the Content-Type HTTP response header do?

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). In responses, a Content-Type header provides the client with the actual content type of the returned content.


2 Answers

Apache: add to your .htaccess file in root directory:

AddDefaultCharset UTF-8 

It will modify the header from this:

Content-Type text/html

...to this:

Content-Type text/html; charset=UTF-8


nginx [doc] [serverfault Q]

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

add charset utf-8; to server block (and reload nginx config)

like image 169
NXT Avatar answered Oct 14 '22 20:10

NXT


When i added this, my response header looked like this:

HTTP/1.1 200 OK
Content-Type: text/html,text/html;charset='UTF-8'
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5

like image 25
Luuk Avatar answered Oct 14 '22 20:10

Luuk