Add Expires Headers for Specific Images

自作多情 提交于 2019-12-01 16:36:56

You can use a FilesMatch, eg.

<FilesMatch "\.(js|css)$">
  ExpiresActive on 
  ExpiresDefault "access plus 1 month"
</FilesMatch>

Or for some specific files:

<FilesMatch "^(example.js|sample.css)$">
  ExpiresActive on 
  ExpiresDefault "access plus 1 month"
</FilesMatch>

Note that using ExpiresDefault for specific files will not work if you already used ExpiresByType. You need to use ExpiresByType again.

So this will NOT work (service-worker.js would still have +1 year expiry):

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"
    ExpiresByType application/javascript                "access plus 1 year"
    <FilesMatch "^(service-worker.js)$">
        ExpiresDefault                                  "access plus 0 seconds"
    </FilesMatch>
</IfModule>

But this will work (service-worker.js will have +0 seconds expiry):

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"
    ExpiresByType application/javascript                "access plus 1 year"
    <FilesMatch "^(service-worker.js)$">
        ExpiresByType application/javascript            "access plus 0 seconds"
    </FilesMatch>
</IfModule>

You might also use Header unset Expires. This would remove Expires header no matter what was set above it. You should also modify (or remove) Cache-Control header. It seems that mod_expires sets both.

    <FilesMatch "^(service-worker.js)$">
        Header unset Expires
        Header set Cache-Control "max-age=0"
    </FilesMatch>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!