htaccess UTF-8 encoding for .html, .css, .js - Whats the best way?
Asked Answered
B

2

22

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
Bonze answered 14/11, 2012 at 13:57 Comment(0)
H
32

I think

AddDefaultCharset utf-8

is enough for all.

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

Hellion answered 14/11, 2012 at 15:19 Comment(6)
Reading more I see a few respected sites saying... AddDefaultCharset UTF-8 will serve all files regardless of file extension. Time to test I guessBonze
@Bonze I am using that, never the less, the lack of a charset is detected on my css and js files on 3rd party tools.Encasement
Adding this to an xml file didn't used the utf-8. Adding AddCharset UTF-8 .xml did. See this.Brann
The documentation 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.Levitate
Where in my .htaccess do I need to place it? Should it go at the top, bottom or below something else?Tasia
@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.Maishamaisie
A
2

My problem was for Persian(فارسی) text in js files which was showing like (عبارت)

I added the code below in .htaccess file, and my problem was solved! :

<filesMatch "\.(html|js)$">
     Header set Content-Type "text/x-java-source; charset=UTF-8"
</FilesMatch>
<FilesMatch "^[Mm]akefile$">
     Header set Content-Type "text/plain; charset=UTF-8"
</FilesMatch>

for me AddCharset UTF-8 .js and AddDefaultCharset utf-8 didn't help

Allusive answered 16/5, 2023 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.