开发者

ForceType text/html;charset=utf-8 in .htaccess is causing all externally linked CSS to stop rendering

I'm using:

ForceType text/html;charset=utf-8

in my .htaccess file, but it's causing all externally linked CSS to stop rendering on their respective pages.

So something like this:

<link rel="stylesheet" type="text/css"开发者_运维百科 href="somestyles.css" media="all" />

no longer works on the page using it.

I've also been trying combinations of:

AddCharset utf-8 .html
AddCharset utf-8 .htm
AddCharset utf-8 .css
AddCharset utf-8 .js
ForceType text/html;charset=utf-8
ForceType text/css;charset=utf-8

but no luck so far. Does anyone know what's wrong?


This is all you need to get UTF-8 for both CSS and Javascript files:

AddDefaultCharset utf-8
AddCharset utf-8 .css .js


This works for me on my web host (in my root .htaccess):

#for html, php
AddDefaultCharset UTF-8
#for css, js
AddType 'text/css; charset=UTF-8' css
AddType 'application/x-javascript; charset=UTF-8' js

I'm not sure, but I think the ForceType may be telling the browser that the css file is html, not css.

UPDATE:

First of all, application/x-javascript should not be used anymore; it should be application/javascript (x- meant experimental). But it's even simpler than that, at least for Apache 2.2.22

#for html, php
AddDefaultCharset UTF-8
#for css, js, etc.
AddCharset UTF-8 .js .css

Using AddType also sets the MIME type, which Apache is likely to take care of correctly, while my original answer set it incorrectly for javascript (as of today). So let Apache do its job on MIME types and use the shorter and simpler single-line AddCharset for all additional types.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜