【问题标题】:https caching of resources used by main php script, NOT the script itselfhttps 缓存主要 php 脚本使用的资源,而不是脚本本身
【发布时间】:2010-12-11 23:32:45
【问题描述】:

我在这里有点困惑。我阅读了一些关于 https 缓存的早期问题,但我没有得到明确的答案。

我有一个脚本:https://www.example.com/main.php

它会生成一个 html 页面,该页面引用位于(相对)以下位置的 images/css/js 资源: /css /javascript /images /a/b/img2

如何为这些资源启用缓存?我有权修改 main.php 脚本的标头输出。


编辑:解决方案如下:

#Set a far expiration date for components
<ifmodule mod_expires.c>
ExpiresActive On
  <filesmatch "\.(jpg|jpeg|gif|png|css|js)$">
       ExpiresDefault "access plus 6 months"
   </filesmatch>
</ifmodule>

#add ETag for components
FileETag MTime Size

【问题讨论】:

    标签: php caching https header http-headers


    【解决方案1】:

    如果您正在运行 Apache Web 服务器,您可能需要一个 .htaccess 文件来输入有关您的组件的缓存信息。

    在 .htaccess 文件中:

    #Set a far expiration date for components
    <ifmodule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/gif A2592000
    ExpiresByType image/png A2592000
    ExpiresByType image/jpg A2592000
    ExpiresByType image/jpeg A2592000
      <filesmatch "\.(jpg|gif|png|css|js)$">
           ExpiresDefault "access plus 10 years"
       </filesmatch>
    </ifmodule>
    
    #add ETag for components
    FileETag MTime Size
    

    访问 main.php 脚本的标头输出只能修改主脚本的缓存,而不是组件。

    【讨论】:

    • 嗨 Mauris,我正在阅读关于 mod_expires 的内容,看起来它会很好用!但我对 jpg|gif|png 上的 filesmatch 命令感到困惑 - 你为什么使用它?
    • 我只希望我的组件被缓存,而不是我的 php 脚本,因为内容可能是动态的。
    • 很抱歉在这里成为一个混蛋并追求这一点(顺便说一句 - 你的解决方案有效!)...但是 jpg|gif|png 不是多余的吗?我们已经在 ExpiresByType 条目中指定了它,不是吗?
    • 好吧,你可以再说一遍 =D - 不记得我为什么这样做了。复制自旧项目。
    • 哦,是的,ExpiresByType 只适用于图像,文件匹配中仍然有 css 和 js
    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2015-07-29
    • 2014-09-20
    相关资源
    最近更新 更多