如何在.htaccess文件中设置一些PHP标头

所以我在一些
PHP文件中有这些标题:

<?php
    header('Content-Type: text/html; charset=UTF-8');
    header('Content-Style-Type: text/css');
    header('Content-Script-Type: application/javascript');
    header('HTTP/1.1 200 OK');
    header('Content-language: en-US');
    header('X-Powered-By: PHP/5.2.17');
    header('Last-Modified: Tue, 01 Jan 2013 00:00:00 GMT');
    header('Cache-Control: no-store, no-cache, max-age=0, must-revalidate');
    header('Pragma: no-store, no-cache, max-age=0, must-revalidate');
    header('Expires: Tue, 01 Jan 2013 00:00:00 GMT');
?>

如何在我的.htaccess文件中设置它们? (应该只针对特定文件),这是我到目前为止所得到的:

<FilesMatch "^(index.php|about.php|contact.php)$">
    # HTTP Headers should be set in here
</FilesMatch>

最佳答案 您需要安装mod_headers,否则可能会收到错误500.您可以在条件中包装标头设置以检查mod_headers是否存在.

<FilesMatch "\.(html|htm|js|css)$">
    <ifModule mod_headers.c>
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Expires "Thu, 1 Jan 2015 05:00:00 GMT"
    </ifModule>
</FilesMatch>

您也可以取消设置标题.例如,如果您在加载balacer后面,真实的访问者IP可以以您不希望向应用程序公开的自定义标头的形式传递.

Header unset Real-Visitor-IP

除了expiration和etags以及特定于Web服务器的自定义标头之外,您不应该在Web服务器级别篡改标头.其中大多数应该在应用程序级别进行管理,如果您传递更改的内容类型,则会阻止某些框架正常工作.

点赞