UTF-8 Character Encodings in HTML and CSS via htaccess – Easy!

with No Comments

Years ago I fell in love with UTF-8.  More recently I have developed a love for HTML5 and CSS3.  I have found that as I am teaching these, my students have been confused about how to set Character Encodings (and Content-Language).  So I decided to make an easy to follow tutorial on how to set both of these in your htaccess file.

 

Some Assumptions

I am talking about HTML5 and CSS and UTF-8, so I will assume these are what we are using.  Most of this post would apply to other Character-Encodings and Content-Language settings as well (just different).  However, this is what I use and if you need something else, you will likely know what that is already!

The Second Assumption is that you have a valid and working htaccess file.  Some file servers do not have their htaccess configured properly.

 

When?Doze

If you are using a When?Doze server (including many WAMP configurations, be sure your htaccess is configured properly.  Using Apache on Unix/Linux/BSD out of the box should work nicely.  If you are using a server that runs on When?Doze I cannot answer your question, When?Doze makes me sleepy.

My advice is to move to a real server (not When?Doze) and save yourself a lot of pain, suffering and torture.  If your boss says you MUST use a When?Doze for the company server, my advice is to get a different job before it ruins you life.

 

Simple UTF-8 and US-en

With the above in mind, here is my simple way of getting rid of over 75% my debugging time and making my life better!

# Default = .php .html .htm .txt
AddDefaultCharset utf-8

# Add a few more... .css .js .xml
AddCharset utf-8 .css .js .xml

# Adds en-US to all file types by default
DefaultLanguage en-US

 

AddDefaultCharset utf-8

This adds the UTF-8 Character Encoding to most default pages. This would be your .html and .php files, as well as plain .txt files (and I am quite sure .htm files also, thought I never use .htm).

The Default Charset does NOT, however, mean all files. notably it does not test to include files with the .css, .js, and .xml extensions.  For that we will need the next command…

 

AddCharset utf-8 .css .js .xml

This adds the Charset utf-8 to the files types listed.  Because I use the text editor Geany, I have it set to automatically encode all files to UTF-8.  So my JavaScript, Cascading Style Sheets and XML Sitemaps are all uniformly coded to UTF-8.  This saves in debugging!

 

DefaultLanguage en-US

This does set the Default Language for all files to “en-US”

 

HTACCESS

By just putting those lines at the beginning of my properly configured htaccess file, solves all the problems if UTF-8 and language encoding. It works in the folder it is placed in, and also in all sub-folders too!
 

Leave a Reply