logo

utf-8
par .htaccess

 

 

17 juillet 2008. Source:

http://www.w3.org/International/questions/qa-htaccess-charset

Setting charset information in .htaccess

The following shows an example of an HTTP header that accompanies a file sent to a user agent. In this case the character encoding information is included in the Content-Type header:

Content-Type: text/html; charset=utf-8

The Content-Type header expresses both the MIME type of the file and the character encoding. The MIME type describes the format of the file being served. HTML files are typically served as text/html. The character encoding (or 'charset') of this file is UTF-8.

If the server is set up to allow users or administrators to change information in .htaccess files, these can provide a way to override default settings.

Specifying by extension

Use the AddCharset directive to associate the character encoding with all files having a particular extension in the current directory and its subdirectories. For example, to serve all files with the extension .html as UTF-8, open the .htaccess file in a plain text editor and type the following line:

AddCharset UTF-8 .html

The extension can be specified with or without a leading dot. You can add multiple extensions to the same line. This will still work if you have file names such as example.en.html or example.html.en.

The example will cause all files with the extension .html to be served as UTF-8. The HTTP Content-Type header will contain a line that ends with the 'charset' information as shown in the example that follows.

Content-Type: text/html; charset=UTF-8

All files with this extension in all subdirectories of the current location will also be served as UTF-8.

Note: It is also possible to achieve the same result using the AddType directive, although this declares both the character encoding and the MIME type at the same time:

AddType 'text/html; charset=UTF-8' html

The decision as to which is most appropriate will depend in part on how you are using extensions for content negotiation. If you are using different extensions to express the document type and the character encoding, this is less likely to be appropriate.

Changing the occasional file

Let's now assume that you want to serve only one file as UTF-8 in a large directory where all the other older files are correctly served as ISO-8859-1. The file you want to serve as UTF-8 is called example.html. Open the .htaccess file in a plain text editor and type the following:

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

What we did here was wrap the directive discussed in the previous section in some markup that identifies the specific file we are concerned with.

Note: It is also possible to achieve the same result using the AddType directive shown above, or, in this case, the ForceType directive, although these declare both the character encoding and the MIME type at the same time:

<Files "example.html">
ForceType 'text/html; charset=UTF-8'
</Files>

Note: Any files with the same name in a subdirectory of the current location will also be served as UTF-8, unless you create a counter directive in the relevant directory.

Tutoriel de .htaccess pour Apache 2

http://httpd.apache.org/docs/2.0/howto/htaccess.html