How do i set expiration on CSS, JS and Images

2020-05-11 21:19发布

I have recently analysed my website with pagespeed addon on firebug. It suggested me to set expiration on CSS, JS and image files.

I am wondering how do i do this?

4条回答
够拽才男人
2楼-- · 2020-05-11 21:47

i would like to add this solution for those searching for it....

its also works great...using .htaccess

https://webmasters.stackexchange.com/a/5275/37765

 <FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
  ExpiresActive On
  ExpiresDefault A2592000
 </FilesMatch>
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-05-11 21:47

What I do is to create a file "expires.conf" and include it in the site file configuration of Apache. You can include in .htaccess if you want. My expires:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType text/html "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 1 month"
</IfModule>

You need to activate the expires module in apache.

查看更多
我只想做你的唯一
4楼-- · 2020-05-11 21:50

Better one (found at http://www.paulund.co.uk/set-expire-headers-in-htaccess, but as 0 seconds didnt work, I've changed it to 1 seconds )

# These are pretty far-future expires headers
# They assume you control versioning with cachebusting query params like:  <script src="application.js?20100608">
# Additionally, consider that outdated proxies may miscache
#
#   www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
# If you don`t use filenames to version, lower the css and js to something like "access plus 1 week"

<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 1 seconds"


# Your document html
  ExpiresByType text/html                 "access plus 1 seconds"

# Data
  ExpiresByType text/xml                  "access plus 1 seconds"
  ExpiresByType application/xml           "access plus 1 seconds"
  ExpiresByType application/json          "access plus 1 seconds"

# RSS feed
  ExpiresByType application/rss+xml       "access plus 1 hour"

# Favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week"

# Media: images, video, audio
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpg                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"
  ExpiresByType video/webm                "access plus 1 month"

# HTC files  (css3pie)
  ExpiresByType text/x-component          "access plus 1 month"

# Webfonts
  ExpiresByType font/truetype             "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# CSS and JavaScript
  ExpiresByType text/css                  "access plus 1 seconds"
  ExpiresByType application/javascript    "access plus 1 month"
  ExpiresByType text/javascript           "access plus 1 month"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>
查看更多
疯言疯语
5楼-- · 2020-05-11 22:04

This is the one I use to fix the exact same thing when I ran the PageSpeed Addon:

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
</FilesMatch>

This goes into your .htaccess file.

Read up on this page for more information about how to set cache for additional file types and/or change the cache length:

http://www.askapache.com/htaccess/apache-speed-cache-control.html

查看更多
登录 后发表回答