apache .htaccess 中的 ForceType text/html;charset=utf-8 导致所有外部链接的 CSS 停止呈现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2489454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:55:26  来源:igfitidea点击:

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

apache.htaccess

提问by chimerical

I'm using:

我正在使用:

ForceType text/html;charset=utf-8

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.

在我的 .htaccess 文件中,但它导致所有外部链接的 CSS 停止在各自的页面上呈现。

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?

但到目前为止还没有运气。有谁知道出了什么问题?

回答by August Karlstrom

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

这是为 CSS 和 Javascript 文件获取 UTF-8 所需的全部内容:

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

回答by Slashback

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

这适用于我的网络主机(在我的根 .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.

我不确定,但我认为 ForceType 可能会告诉浏览器 css 文件是 html,而不是 css。

UPDATE:

更新:

First of all, application/x-javascriptshould 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

首先,application/x-javascript不应再使用;它应该是application/javascriptx-意味着实验)。但它甚至比这更简单,至少对于 Apache 2.2.22

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

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

UsingAddType还设置了 MIME 类型,Apache 可能会正确处理该类型,而我的原始答案为 javascript设置错误(截至今天)。因此,让 Apache 在 MIME 类型上完成它的工作,并AddCharset为所有其他类型使用更短更简单的单行。