Specify Character Set in .htaccess

I usually add the character set coding within the header of a website. I guess this is the general practice by everyone.

I read that it was possible to add the language coding to the .htaccess file.

This would enable configuring for all files. There would be a marginal gain in a small number of characters removed from each downloaded page. The element within the page isn’t normally cached like images, CSS and JavaScript files.

To define the character set for all files can simply add the specification to the file. For example to use utf-8 encoding for anything served text/plain or text/html.

AddDefaultCharset 8

As this applies to all the content served by the web site, we may wish to be more restrictive, perhaps to a single file type:

AddCharset utf-8 .html .css .js .xml .json .rss

Or

AddCharset UTF-8 .html

Its also possible to implement a restriction for a single file:

<Files "example.html">
AddCharset UTF-8 .html
</Files>

You may wish to mix the definition of the default character set, together with alternative sets for some file types, for example the CSS and JavaScript.

AddDefaultCharset UTF-8 #all files set to UTF-8
AddCharset ISO-8859-1 .css .js

Some time later after having added the above and removed the setting from within the HTML tag of the website I ran one of the online website analysis sites. One of the results was that the character set was undefined.

So having read that it was efficient to add the character set within the .htaccess file as opposed to the individual website pages, for speed and efficiency I find that testing the site flags it as an issue.

Is there more info regards Google’s take on this? I’ve searched for information to confirm, or otherwise, the location of the character set but nothing to confirm one way or the other.

My assumption is that both are valid, therefore the choice is yours – just be sure to set the character set appropriately.

References

W3C – Setting charset information in .htaccess