Set Expires

Setting "expires" tells browsers downloading these files that they don't need to request it again for this specific length of time. In otherwords, use the cache instead if you have it. This can reduce stress on the server for you, and speed up page load time for visitors.

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 10 days"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-icon "access plus 1 year"


  1. OldGuy
    Permalink to comment#

    I would add that if you are using something like that, embed a datestamp or version identifier in the filenames of resources. That way, if you update a resource you can increment the indicator so that visitors will load the new one instead of continuing to use the old one until the expires period runs out.

    • Joe
      Permalink to comment#

      Yes! I’ve found this to be critical with flash files.

    • Jacob Dubail
      Permalink to comment#

      Can you offer a snippet that would do just that? Sounds like a great idea… but I wouldn’t know how to do it.

    • TeMc
      Permalink to comment#

      This increment thing the two above you are talking about is likely simpler then you think.

      It goes like this:

      [link rel="stylesheet" href="http://path/style.css?v=001" type="text/css" /]

      those of you knowing PHP will recognize the v-parameter and “001” as a value. Now… what does PHP has to do with CSS exactly ?

      Answer: Nothing !

      However the browser doesn’t know that and will think that style.css?v=001 is not the same as style.css?v=002 . So, use the snippet above from Chris Coyier and append a v-parameter to the url and whenever the v-parameter changes the cache/setExpires will be ingnored and reset to 0.

      Also, if you’re using this in WordPress, I recommend not setting it manually in header.php but make a function in functinons.php like so:


      function version(){
      $version = "001";
      return $version;

      # header.php

      href="style.css?v=[?php echo version(); ?]"

    • NetHawk
      Permalink to comment#

      This is what Google says:

      Don’t include a query string in the URL for static resources.

      Most proxies, most notably Squid up through version 3.0, do not cache resources with a “?” in their URL even if a Cache-control: public header is present in the response. To enable proxy caching for these resources, remove query strings from references to static resources, and instead encode the parameters into the file names themselves.

  2. Matthew
    Permalink to comment#

    I’ve always wondered—is there a way to turn this code off in a sense if you decide to make updates to your Javascript or image files for example? I’ve read that you have to rename them, but is there a way around this or do you have to wait for the cached time to run out?

  3. muheeb
    Permalink to comment#

    Thanks to the team of, I appreciate your help, Your website is very resource able. I learned many tricks and tips and still learning.

    I have one problem need help please. I am unable to display images from my website in google chrome browser. It works fine in IE and Firefox. When I was researching regarding this issue, I learned that we have to set expire for the images. Can any one please guide me how to set expiry for the images in CSS or else any other way.

    Thanks for your all support.

  4. Bilal
    Permalink to comment#

    This code is not working on my site . Tell me why?

  5. bob
    Permalink to comment#

    really nice snippet.
    This website is a great resource. no doubt!

    Just a quick question: What is the difference between “ExpiresByType application/x-javascript” and “ExpiresByType application/javascript”?

    What is “x-javascript” exactly?


  6. Thanks and q's
    Permalink to comment#

    jpg was left out of the filetype list, might save some confussed people some time to add it. Took me a few hours to notice! or is there something i dont know? are jpegs the same as jpgs, well i suppose they are but are they to the server?

  7. removals
    Permalink to comment#

    jpg was left out of the filetype list, might save some confussed people some time to add it. Took me a few hours to notice! or is there something i dont know? are jpegs the same as jpgs, well i suppose they are but are they to the server i have tha some question

  8. Mladen Petrov
    Permalink to comment#

    Excellent idea. Thanks for sharing

  9. Karl
    Permalink to comment#

    I have use this code in a couple of website, but it is add, it does not get recognized at all.

    This is what I get for example in Yslow:

    Grade F on Add Expires headers

    What would prevent this code from been added to the expired rules?


  10. mohammad
    Permalink to comment#

    Hi , I’ve a file with .css.php extension that didnt take those rules above.
    What I should do for this file ????

    • Ray


      make sure your PHP sends the CSS with the correct MIME type. Usually with a header("Content-type: text/css"); at the top of your script.

  11. XMPP texting replaces sms
    Permalink to comment#

    that is too primitive — and problematic

    it needs to be relative to file modification time assuming you upload with that information intact

  12. Saku Mättö
    Permalink to comment#

    Thanks for the Set Expires tip. I got my site boosted over the A-grade on gtmetrix thanks to this tip.

  13. Elena Popescu
    Permalink to comment#

    This page allows you to inspect the HTTP headers sent back by any web server, so you can check page expiration, caching settings and server version.

  14. Vineesh C
    Permalink to comment#

    I have set the expiration to my site. thanks a lot. now pagespeed shows good.

  15. Artemisia
    Permalink to comment#

    How do I set this in a blog?
    I don’t have access to the .htaccess file.
    Can I set these as a meta tag in my header?

  16. Anand Vaishnav
    Permalink to comment#

    I have added the rule in my .htaccess file for CSS

    ExpiresByType text/css “access plus 1 week”

    But the css file are using version like front.css?ver=4.1
    and it doesn’t reflect any expiry

    Below are the details

    There are 48 static components without a far-future expiration date.
  17. Unknown
    Permalink to comment#

    @Anand Vaishnav these tags does not add any expiry date time on you urls for file. It would only request them after that time. 1 week in your case

    • Anand Vaishnav
      Permalink to comment#

      Thank You for your reply :) Well This is downgrading my performance rating so I was worried. Thank Again :) Will it mess any thing if I remove the version details from the end of URL ??

  18. Rick

    I do not have access to my .htaccess file.

    Is there a way to add some code right in the web page for my .jpg images only?

    If so, could give me the sample code?


  19. sb
    Permalink to comment#

    I have used the .htaccess expiry but google page speed still shows
    Consider Fixing:
    Leverage browser caching
    Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

    and list all the images and js under it but the css is not being shown in the list. Any idea what is wrong. here is my code


    ExpiresActive On
    ExpiresByType text/css “access plus 1 week”
    ExpiresByType text/plain “access plus 1 month”
    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 text/html “access plus 1 month”
    ExpiresByType text/php “access plus 1 month”
    ExpiresByType text/x-javascript “access 1 month”
    ExpiresByType application/x-javascript “access plus 1 month”
    ExpiresByType application/javascript “access plus 1 week”
    ExpiresByType application/x-icon “access plus 1 year”
    ExpiresDefault “access plus 1 month”


  20. Pablo
    Permalink to comment#

    This link might help you refine your htaccss file to get a better score on the Google page insight.

  21. merans
    Permalink to comment#

    hope this help >.<

    RewriteEngine On

    BEGIN Mod Header

    ExpiresActive On

    Turn on Expires and set default expires to 10 years

    END Mod Header

    BEGIN Cache Control

    Header set Expires “Thu, 15 Apr 2012 20:00:00 GMT”
    Header unset ETag
    FileETag None

    #END Cache Control

    Expires Caching

    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 plus 10 years”


  22. Asif
    Permalink to comment#

    Hey buddies,
    I want to add expiration of a specific .js cdn in .htaccess
    cdn is here
    Please tell me how can i do that?
    I m speeding my website and doing leverage browser caching in .htaccess.

  23. HEDOMA Group
    Permalink to comment#


    its possible to add a variable Expire in meta link ?

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.