PHP:如何提供正确的内容类型(取决于文件扩展名?)?
我正在使用下面的脚本来 gzip 一些 CSS 和 JS 文件。它工作得很好 - 除了使用错误的内容类型(而不是适当的 application/x-javascript)提供 JS 文件。如何改进 PHP 代码以提供正确的内容类型?谢谢!
.htaccess:
AddHandler application/x-httpd-php .css .js
php_value auto_prepend_file gzip.php
gzip.php:
<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
I'm using the scripts below to gzip some CSS and JS files. It works nice - except for serving JS files with the wrong content-type (and not the appropriate application/x-javascript). How could I improve the PHP code in order to serve the right content-type? Thanks!
.htaccess:
AddHandler application/x-httpd-php .css .js
php_value auto_prepend_file gzip.php
gzip.php:
<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查请求的 URL 路径的结尾:
You could check the ending of the requested URL path:
您还可以通过 FileInfo 检查 mime 类型,而不是使用扩展名。
You could also check the mime-type thanks to FileInfo rather than using the extension.