是否可以为Apache中的单个文件夹设置Content-type输出值?


Is it possible to set the Content-type output value for a single folder in Apache?

我的服务器设置为UTF-8,但我想要一个特定的文件夹输出一个Content-Type HTTP报头,其中说charset=Shift-JIS。我试图上传一个。htaccess文件到文件夹,包含AddDefaultCharset Shift-jis,但这不起作用。

您是否将AddDefaultCharset指令放置在位于您试图更改Charset的目录中的htaccess中?如果没有,请这样做。

如果您希望该目录下的所有html文件都具有该字符集,请尝试

<FilesMatch "'.html?$">
    ForceType 'text/html; charset=Shift-jis'
</FileMatch>

如果这不起作用,你可以直接设置HTTP头与以下指令(在同一个htaccess)

<FilesMatch "'.html?$">
    Header add Content-Type "text/html; charset=Shift-jis"
</FilesMatch>

您可能需要为其他文件类型添加更多的FileMatch容器,并调整标题内的MIME类型(例如:"text/css"或"application/javascript" for .css.js文件)

根据@MiffTheFox是的。

<Files ~ "'.html?$">  
     Header set Content-Type "text/html; charset=utf-8"
</Files>

请查看原始答案https://stackoverflow.com/a/913880/1431184