开发者

How do I set expiration on CSS, JS and Images?

开发者 https://www.devze.com 2023-01-24 21:24 出处:网络
I have recently analysed my website with pagespeed addon on firebug. It suggested me to set expiration on CSS, JS and image files.

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开发者_如何学Python wondering, how do I do this?


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


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>


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.


Add web.config file in your solution to remove "expiration not specified"

<configuration>
<system.webServer>
<staticContent>
  <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
</configuration>


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

精彩评论

暂无评论...
验证码 换一张
取 消