dsy19890123 2014-07-13 14:15
浏览 57
已采纳

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

So I have these headers in some of my PHP files:

<?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');
?>

How do I set them in my .htaccess file? (Should be for specific files only), here's what I got so far:

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

1条回答 默认 最新

  • douyue1926 2014-07-13 14:25
    关注

    You need to have mod_headers installed, or you might get error 500. You can wrap the setting of headers in a condition to check if mod_headers is present or not.

    <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>
    

    You can also unset headers. For example, if you're behind a load balacer the real visitor IP could be passed in form of a custom header you don't want to expose to the application.

    Header unset Real-Visitor-IP
    

    Besides expiration and etags, and webserver specific custom headers, you shouldn't tamper with headers at webserver level. Most of them should be managed at the application level, and you would prevent some frameworks from working normally if you pass them altered content types.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电脑出问题了,说是usbmom注册表没
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网