Apache为.js文件发送错误的响应头

时间:2013-01-16 16:43:28

标签: javascript apache mime-types content-type

我注意到Chrome报告的.js文件的MIME类型错误。这些文件是使用Apache 2提供的,Apache 2最初没有在其conf文件中引用js文件。

我将以下配置添加到http.conf:

AddType application/x-javascript .js

然后重新加载配置。 .js文件仍在使用此响应标头发送:

HTTP/1.1 200 OK
Date: Wed, 16 Jan 2013 16:39:42 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.4.10
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

没有.htaccess文件。

什么会导致Apache发送不正确的Content-Type,即使我在配置中指定了application / x-javascript并重新加载了服务器?

2 个答案:

答案 0 :(得分:1)

我最近在另一台服务器上遇到了这个并解决了这个问题。 conf.d/php.conf包含以下行:

AddHandler php5-script .php .phtml .html .css .js
AddType text/html .php

我将第一行改为

AddHandler php5-script .php .phtml .html
AddType text/html .php

然后保存并重新启动Apache。 js&现在,css文件在Firefox / Chrome中以正确的mime类型显示。

顺便说一下,我尝试在AddType text/javascript .js下方添加AddType text/html .php,但在Inspector中文件仍然显示为text/html。我必须从AddHandler中删除.css和.js才能发送正确的mime类型。

答案 1 :(得分:0)

如果你的httpd.conf中有SetHandler application/x-httpd-php之类的内容,那么你应该把它放到<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>中,让Apache为其他文件发送好的Content-Type。