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

3

28

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!

Biancabiancha answered 1/11, 2011 at 7:1 Comment(0)
N
44

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)

Noun answered 15/1, 2013 at 13:23 Comment(2)
I know it should simply work but my HTTP Header still just respond with Content-Type text/html. I don't want to use a <meta> tag in my document to declare the character encoding. I use Apache 2.4.Smriti
@Smriti make sure the server process .htaccess directives and the file is located in the proper directoryNoun
C
3

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

Coca answered 27/12, 2014 at 13:41 Comment(1)
UTF-8 seems to be the default for IIS (technet.microsoft.com/en-us/library/cc771836%28v=ws.10%29.aspx)Coca
T
1

With Apache, you use http://httpd.apache.org/docs/2.2/mod/core.html#adddefaultcharset

With IIS you edit the MIME type for the filetype in the list of files.

With most server-side technologies like PHP or ASP.NET there's a method or property provided by that technology. For example in ASP.NET you can set it in config, page, or page code-behind.

Ticon answered 12/12, 2011 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.