21

For yslow page speed I want to remove my meta tag and put my encoding into the .htaccess file. Below are all the ways to do it I have read about. Which is the preferred way? Also is the language setting a good idea too - and if out side of the filesmatch will it apply to all file types?

1) https://github.com/jancbeck/My-Wordpress-Boilerplate/blob/master/htaccess.txt

AddDefaultCharset utf-8
AddCharset utf-8 .html .css .js
DefaultLanguage en-US

vs

2) http://www.askapache.com/htaccess/using-http-headers-with-htaccess.html

<filesMatch "\.(html|css|js)$">
AddDefaultCharset UTF-8
DefaultLanguage en-US
</filesMatch>

vs

3) I suspect this is all that's needed. But untested.

AddCharset UTF-8 .html .css .js
DefaultLanguage en-US
YakovL
  • 5,213
  • 10
  • 46
  • 71
Eric
  • 319
  • 1
  • 2
  • 7

1 Answers1

28

I think

AddDefaultCharset utf-8

is enough for all.

Maybe better way is set encoding to files, which are using different charset than default.

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
cjayho
  • 489
  • 4
  • 6
  • 2
    Reading more I see a few respected sites saying... AddDefaultCharset UTF-8 will serve all files regardless of file extension. Time to test I guess – Eric Nov 14 '12 at 18:49
  • @Eric I am using that, never the less, the lack of a charset is detected on my css and js files on 3rd party tools. – Andres SK Apr 03 '15 at 16:00
  • Adding this to an xml file didn't used the utf-8. Adding `AddCharset UTF-8 .xml` did. See [this](http://stackoverflow.com/a/913884/1057527). – machineaddict May 04 '15 at 07:08
  • 5
    The [documentation](https://httpd.apache.org/docs/2.4/en/mod/core.html#adddefaultcharset) says "This directive specifies a default value for the media type charset parameter (the name of a character encoding) to be added to a response **if and only if the response's content-type is either text/plain or text/html**". So this is NOT enough for all. – yankee Nov 01 '16 at 12:12
  • Where in my .htaccess do I need to place it? Should it go at the top, bottom or below something else? – Richard Young Nov 17 '16 at 14:00
  • @RichardYoung Unless there is another AddDefaultCharset directive in the .htaccess file, it does not matter. I personally place mine at the top first, with a explanatory comment #explaining why it's used. For urgent fixes placing at the bottom might work better, as there may be another AddDefaultCharset directive, and Apache will honor the last usage of a directive. – Johnathan Elmore Jan 20 '17 at 20:00