开发者

Change Charset Response Header for Apache Indexes

I'm trying to set up windex on my local apache server for changing the auto-generated file indexes; some of my files and folders have utf-8 characters such as ».

This shouldn't be a problem, as the html header has the charset set to utf-8. The php files have been encoded in utf-8. I've even set 'AddDefaultCharset' to off in my httpd.conf for the Apache server, but when I try to load any page, I get 'Content-Type:text/html;charset=ISO-8859-1' in the response header.

What is causing this, and how d开发者_运维百科o I stop it?


Turns out that Apache has a separate setting for encoding the indexes it generates. You can set the charset for an index by adding this line to an .htaccess file, placed in the directory where your index will be generated:

IndexOptions Charset=UTF-8

This fixed it for me!


AddDefaultCharset is used when a response header's content-type is text/plain or text/html
In .htaccess or httpd.conf you can add:

AddDefaultCharset utf-8

AddCharset is to define the character set for any given file extension.

In .htaccess or httpd.conf you can add:

AddCharset utf-8 .html .css .php .txt .js

In PHP (before any page content is output):

<?php
header('Content-Type: text/html; charset=utf-8'); 

If you HAVE to use the HTML meta tag (cannot edit config or htaccess) it must be the first thing that follows the <head>
Reference: Best Practice: Get your HEAD in order

<head><meta http-equiv="content-type" content="text/html; charset=UTF-8">


For html
Put the following between your <head> </head> tags:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

For xhtml
Put the following between your <head> </head> tags:

<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/>

Begin your php files with:

<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜