How to Add mod_expires to your .htaccess

When visiting a website, the website will cache in your browser. This cache can be controlled by configuring your Cache-Control HTTP headers for your website This is done by adding mod_expires in the .htaccess file of your server.

If you don’t set your Cache-Control for the HTTP headers, then, you will have a longer wait times when visiting your website. Each time your website is accessed without Cache-Control, your website has to make a request to the server for each image, javascript file, CSS file, and so forth to load.

Browser caching is good because it allows your web resources to be stored in the browser for faster page load time. This makes your site faster to surf on the web. To remove the cache from your browser, your browser cache needs to be cleared. For information on clearing your browser cache, please see our article on How to clear your browser’s cache. The following sections will explain the basics of setting up the Apache Module mod_expires in your .htaccess.

Basic code for setting expire dates for cache

In order to add browser caching to your website, you will need to set the date for when the cache expires. This cache code is placed in the .htaccess found in your public_html folder. You will need to edit your .htaccess file. Add the following code to the file and save it.

ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"

Now your site will set the time each for each resource that was added to the .htaccess to expire. In the previous code example, the jpg, jpeg, gif, png will expire in a year and pdf, javascript and flash files will expire in a month. More explanation o these settings will come later in this article. Next we will look at using the for adding Cache-Control with mod_expires.

Code for mod_expires in .htaccess

Using mod_expires in .htaccess allows image types and other file types to be set in an array. This matches the file types to the specific expire time. This streamlines the htaccess code. In the code below, the file types are listed in a row like jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3. Below is an example of the code to use.

<filesmatch ".(jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3)$"=""&rt;
ExpiresActive on
ExpiresDefault "access plus 2 days"

This sets the cache to expire to the same duration for each of the file types. To specify the specific times for each individual file type, you will want to use the previous code example. The following will explain the Directive types that are used for Cache-Control.

Directive types

There are three directive types; ExpiresActive, ExpiresByType, and ExpiresDefault. The following table explains the difference between them.

Caching directives
ExpiresActive DirectiveEnables the Expires headers for the website.
ExpiresByType DirectiveThis defines the age of the cache header and the type of file to cache.
ExpiresDefault DirectiveThis sets the age of the cache for all documents other than those specified in the ExpiresByType for the site.

Caching Directive bases

There are 3 base types the access, now, and modification type.

  • access
  • now (same as “access“)
  • modification

Duration of cache time

The duration of cache time can be set to one of the following units of time.

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

Basic syntax for ExpiresByType intervals

The following shows the basic syntax for the ExpiresByType. Each file type can be set to specific times to expire the cache.

ExpiresByType text/html "access plus 2 days 12 hours"
ExpiresByType image/png "access plus 6 months 3 days"

Recommended Expire date ranges

You do not want to set your expire times for your cache to unrealistic settings. If you keep the cache to a maximum of a year and a minimum of a month, you should have your browser caching working optimized for your site.

  • Set your images to a long expire time like “access plus 1 year”. Images take more time to load and are updated less frequently than other files.
  • Make your CSS, HTML, and Javascript expire at a minimum of a month like “access plus 1 month”. CSS, HTML and JavaScript’s typically are updated more when developing a site than the sites images.
  • Keep your cache expire date at most a year.

51 thoughts on “How to Add mod_expires to your .htaccess

  1. Very useful data, I was also looking for something that would speed up the website for me a bit. So how do we use it will also reduce the consumption of our pages because this part will already be in the browser’s memory?

    1. Hi,

      Thank you so much for your question! Yes, your website will load faster because the site resources will already be saved in the browser.

  2. I have just changed to InMotion hosting and I am getting ‘F’ results for leverage browser caching from GTMetrix and was planning on adding mod_expires in the .htaccess file but (a) not sure which one of your two suggestions to add and (2) whether I remove  whats there, add above or below? Any advice would be very appreciated.

    # BEGIN Adaptive Images

    #=======================

     

    <IfModule mod_rewrite.c>

     

        RewriteEngine On

     

        # Watched directories

        RewriteCond %{REQUEST_URI} /wp-content/uploads [OR]

        RewriteCond %{REQUEST_URI} /wp-content/themes

     

        # Redirect images through the adaptive images script

        RewriteRule \.(?:jpe?g|gif|png)$ /wp-content/plugins/adaptive-images/adaptive-images-script.php [L]

     

    </IfModule>

     

    # END Adaptive Images

     

    SetEnv MAGICK_THREAD_LIMIT 1

     

    <IfModule mod_suphp.c>

    suPHP_ConfigPath /home/moleco5/public_html

    </IfModule>

     

     

    # BEGIN WordPress

    <IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteBase /

    RewriteRule ^index\.php$ – [L]

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule . /index.php [L]

    </IfModule>

     

    # END WordPress

    # BEGIN EWWWIO

     

    # END EWWWIO

     

    1. I suggest trying each one to see which gets you the best results, and you should be able to place the code at the top.

  3. https://media.vcommission.com/brand/files/vcm/318/Koovs_MenShirtsStartingAtRs995_300x250.jpg (expiration not specified)
    https://sumome.com/api/load/ (expiration not specified)

    I’m getting the above errors, how can I fix these errors?

    How can I set expiration for external URLs?

    1. Hello Arun,

      Apologies, but I’m not seeing the errors you’re referring to. You can’t set expire times for external URLs. Putting in mod expires is typically done for assets that are loaded into the server and then cached into the browser. For information about Expires, check out: What are expires headers.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  4. John-Paul,

    Thank you for the clarifications.

    It is an excellent article and very helpful. I do think it would be wise to add a line for text/html in the first “Basic code” section, to help people who are cutting and pasting from that section.

    Thanks again,

    Chris

  5. Hello,

    I note two things about the recommended htaccess lines.

    1) Expires for html files are not set.

    HTML5 Boilerplate recommends ExpiresByType text/html  “access plus 0 seconds” This seems like a good strategy to me to set a very low cache time for html files, allowing the super fresh html to pull in any resources with changed file names. Although 0 seconds may be more for development than production.

    Should text/html be added to the code recommendations in this article?

    2) Expires “Basic code” is not wrapped in <ifmodule>

    I assume mod_expires.c is on by default on InMotion servers, so a check is not needed if users aren’t using Filesmatch.

    Is that correct? Is it better practice to wrap Expires lines in ifmodule just in case, or not wrap to avoid tasking the server with the check?

    Thanks,

    Chris

    1. Hello Chris,

      Thank you for contacting us. The section above on “Basic syntax for ExpiresByType intervals” includes HTML. As described above “Make your CSS, HTML, and Javascript expire at a minimum of a month like “access plus 1 month”. CSS, HTML and JavaScript’s typically are updated more when developing a site than the sites images.”

      These are the recommendations as written by the author at that time. Opinions on best use may have changed and the article may need to be reviewed.

      Yes, mod_expires is on by default on our servers. It is difficult to provide “best practice” tips, since the preferred settings can differ, based on your specific site setup and needs. If you want to test, there is a section above that includes the “Code for mod_expires in an .”

      If you have any further questions, feel free to post them below.

      Thank you,
      John-Paul

  6. Hello Dear All, we are having a wordpress site and we have the following .htaccess file code. We are getting Add Expires headers score F (0). Can anyone please let us know how to fix this problem. ? We also tried W3 PlugIn but having the same result. Thank you. 

    .htaccess file is as here.

    # BEGIN WordPress

    Header set Connection keep-alive

    <IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteBase /

    RewriteRule ^index\.php$ – [L]

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule . /index.php [L]

    </IfModule>

     

    # compress text, html, javascript, css, xml:

    AddOutputFilterByType DEFLATE text/plain

    AddOutputFilterByType DEFLATE text/html

    AddOutputFilterByType DEFLATE text/xml

    AddOutputFilterByType DEFLATE text/css

    AddOutputFilterByType DEFLATE application/xml

    AddOutputFilterByType DEFLATE application/xhtml+xml

    AddOutputFilterByType DEFLATE application/rss+xml

    AddOutputFilterByType DEFLATE application/javascript

    AddOutputFilterByType DEFLATE application/x-javascript

     

    # END WordPress

    <IfModule mod_expires.c>

     

        ExpiresActive on

        ExpiresDefault                                      “access plus 1 month”

     

      # CSS

     

        ExpiresByType text/css                              “access plus 1 year”

        

        

     

     

      # Data interchange

     

        ExpiresByType application/atom+xml                  “access plus 1 hour”

        ExpiresByType application/rdf+xml                   “access plus 1 hour”

        ExpiresByType application/rss+xml                   “access plus 1 hour”

     

        ExpiresByType application/json                      “access plus 0 seconds”

        ExpiresByType application/ld+json                   “access plus 0 seconds”

        ExpiresByType application/schema+json               “access plus 0 seconds”

        ExpiresByType application/vnd.geo+json              “access plus 0 seconds”

        ExpiresByType application/xml                       “access plus 0 seconds”

        ExpiresByType text/xml                              “access plus 0 seconds”

     

     

      # Favicon (cannot be renamed!) and cursor images

     

        ExpiresByType image/vnd.microsoft.icon              “access plus 1 week”

        ExpiresByType image/x-icon                          “access plus 1 week”

     

      # HTML

     

        ExpiresByType text/html                             “access plus 0 seconds”

     

     

      # JavaScript

     

        ExpiresByType application/javascript “access plus 1 year”

        ExpiresByType application/x-javascript “access plus 1 year”

        ExpiresByType text/javascript “access plus 1 year”

        ExpiresByType text/css “access plus 1 month”

        ExpiresByType application/pdf “access plus 1 month”

        ExpiresByType text/x-javascript “access plus 1 month”

        ExpiresByType application/x-shockwave-flash “access plus 1 month”

        ExpiresByType image/x-icon “access plus 1 year”

     

     

      # Manifest files

     

        ExpiresByType application/manifest+json             “access plus 1 week”

        ExpiresByType application/x-web-app-manifest+json   “access plus 0 seconds”

        ExpiresByType text/cache-manifest                   “access plus 0 seconds”

     

     

      # Media files

     

        ExpiresByType audio/ogg                             “access plus 1 month”

        ExpiresByType image/bmp                             “access plus 1 month”

        ExpiresByType image/gif                             “access plus 1 month”

        ExpiresByType image/jpeg                            “access plus 1 month”

        ExpiresByType image/png                             “access plus 1 month”

        ExpiresByType image/svg+xml                         “access plus 1 month”

        ExpiresByType image/webp                            “access plus 1 month”

        ExpiresByType video/mp4                             “access plus 1 month”

        ExpiresByType video/ogg                             “access plus 1 month”

        ExpiresByType video/webm                            “access plus 1 month”

     

     

      

    </IfModule>

    <ifModule mod_gzip.c>

    mod_gzip_on Yes

    mod_gzip_dechunk Yes

    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$

    mod_gzip_item_include handler ^cgi-script$

    mod_gzip_item_include mime ^text/.*

    mod_gzip_item_include mime ^application/x-javascript.*

    mod_gzip_item_exclude mime ^image/.*

    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

    </ifModule>

    AddOutputFilterByType DEFLATE text/plain

    AddOutputFilterByType DEFLATE text/html

    AddOutputFilterByType DEFLATE text/xml

    AddOutputFilterByType DEFLATE text/css

    AddOutputFilterByType DEFLATE application/xml

    AddOutputFilterByType DEFLATE application/xhtml+xml

    AddOutputFilterByType DEFLATE application/rss+xml

    AddOutputFilterByType DEFLATE application/javascript

    AddOutputFilterByType DEFLATE application/x-javascript

     

    Header unset ETag

    FileETag none

     

    1. Hello Alam,

      Sorry to hear you’re having problems with the headers. I’m not sure how you have your WordPress installation is configured (caching, etc.). You may want to review this post in the WordPress.org support forum. They discuss the same problem and have possible solutions for you.

      I hope that helps to answer your question! If you require further assistance, please let us know!

      Regards,
      Arnel C.

  7. Hello,

     

    I have gtMetrix report with regarding below issue:

     

    I have set .htaccess file as per above detail but i didn’t get any result. I have clear cache also.

     

    Any suggestion for this?

     

     

    1. Hello Digant,

      The only way you’re going to get rid of the files you’re listing is to NOT make reference or use of those files FROM your website. Gtmetrix is taking a profile of your site by seeing EVERY element that is loading or requries time to load on an internet browser. Your website may contain multiple references to files/fonts/links, etc. that may not even be on your website – but since they require time to load, they will count as a negative in terms of the performance of your website.

      You can’t change the properties of those offsite files, except in how you reference them. If you really want to get them off of gtmetrix report, then remove the dependencies from your website.

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

  8. OK so my htacess is correct and proper?  No changes needed or reccomended?  Thank you for your help I will follow up as you suggest.

  9. I am a newbie, and trying to build my own site.  I have GT metrix saying: There are 6 static components without a far-future expiration date.  Here are those: 

    Here is a copy of my htacess:

    # Redirect old file path to new file path
    Redirect /hatsan-reman/ https://www.airgunproshop.com/hatsan-airguns/

    # ———————————————————————-
    # CORS-enabled images (@crossorigin)
    # ———————————————————————-
    # Send CORS headers if browsers request them; enabled by default for images.
    # developer.mozilla.org/en/CORS_Enabled_Image
    # blog.chromium.org/2011/07/using-cross-domain-images-in-webgl-and.html
    # hacks.mozilla.org/2011/11/using-cors-to-load-webgl-textures-from-cross-domain-images/
    # wiki.mozilla.org/Security/Reviews/crossoriginAttribute
    <IfModule mod_setenvif.c>
      <IfModule mod_headers.c>
        # mod_headers, y u no match by Content-Type?!
        <FilesMatch “\.(gif|png|jpe?g|svg|svgz|ico|webp)$”>
          SetEnvIf Origin “:” IS_CORS
          Header set Access-Control-Allow-Origin “*” env=IS_CORS
        </FilesMatch>
      </IfModule>
    </IfModule>
    # ———————————————————————-
    # Webfont access
    # ———————————————————————-
    # Allow access from all domains for webfonts.
    # Alternatively you could only whitelist your
    # subdomains like “subdomain.example.com”.
    <IfModule mod_headers.c>
      <FilesMatch “\.(ttf|ttc|otf|eot|woff?|woff2?|font.css|css)$”>
        Header set Access-Control-Allow-Origin “*”
      </FilesMatch>
    </IfModule>

    ## EXPIRES CACHING ##
    <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>
    ## EXPIRES CACHING ##

    ##  VARY: ACCEPT-ENCODING HEADER ##
    <IfModule mod_headers.c>
      <FilesMatch “\.(js|css|xml|gz)$”>
        Header append Vary: Accept-Encoding
      </FilesMatch>
    </IfModule>
    ##  VARY: ACCEPT-ENCODING HEADER ##

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

    What did I screw up?  Thank you!!!!

    1. Hello Scott,

      If those static components are NOT going to change because they’re graphics (generally tiny graphics, too) or consistent files, then you should not have to worry about putting an expiration date on them. The Gtmetrix page is for determining how best to optimize your site for speed purposes. In this case, it really won’t make that much of a difference. You can also contact GTMetrix directly for advice if you wish to continue pursuing this particular issue. We’re not sure why they mark those particular items with any large concern for the purpose of the site opimization.

      Regards,
      Arnel C.

  10. I have been tryint to enable Expire headers from my .htaccess, but it is not enabled, I have been add several types of code for expire header. it is not working, Please let me know what i’am in wrong in that

    1. Hello Kumar,

      Sorry for the problem with the expire headers. In order for us to help you we would need to see your .htaccess entries. If you can please provide account information or the .htaccess entries, then we can proceed with troubleshooting the problem.

      If you have any further questions or comments, please let us know.

      Arnel C.

  11. Thank you, JohnPaul!

    On your suggestion to  host those scripts/resources locally… if you take a close look at the specific items I included in my query, they come from StatCounter, googleAnalytics and ShareThis — I don’t believe you can relocate any of them. And, as you said, reverse CDN might introduce other problems.

    I gather there might not be a good solution to this one… but if you find one at a later time, I’d love to hear about it.

    Thanks again,

    JF

  12. Thank you, Amel!

    Yes, I understand what W3TC does, and it has been very effective (my page loading dropped from about 700ms to 220ms after switching from another caching plugin), but my YSlow score still points to areas that need improvement.

    In addition, InMotion Systems staff is telling me that my sites experience Resource Overage… and wanted me to lower my GTmetrix scores, and this is why I’m here with questions.

    Specifically, under Add Expires Headers (YSlow score: E-56), it says that the following links — all static, all going to outside servers — do not have expiration:

    Add Expires headers
    There are 4 static components without a far-future expiration date.

        https://c.statcounter.com/t.php…
        https://www.google-analytics.com/ga.js
        https://wd-edge.sharethis.com/button/getAllAppDefault.esi…
        https://wd-edge.sharethis.com/button/checkOAuth.esi

    I believe that W3TC doesn’t have any effect on these outside links…  So, I’m asking whether there are instructions (code) that I can place in the .htaccess file that will have an effect on links going to outside servers.

    Recently, InMotion staff was able to help me refine my .htaccess file on some other issues, I’m hoping that if you cannot do the same here you would inquire among your colleagues and come up with the code that can do this. Once I have the code structure, I can enter it very easily.

    Thank you again!

    JF

     

    1. Hello JF,

      Thank you for contacting us. I spent some time researching this (and asking around the office) as was not able to find a good working solution.

      The problem is that when you pull something from an external server/source, you are affected by their server performance and load time.

      It may be possible to pull the resources periodically and store them on your server, but it is almost like a reverse cdn setup, and can cause other problems.

      A better solution may be to host the scripts/resources you are pulling externally on the same server your website resides on. Then you have control over the files, and caching, etc.

      Thank you,
      John-Paul

  13. Hi,

    The GTmetrix report for my sites — newly hosted on InMotion — lists files which are not covered by the W3 total Cache WP plugin: all are links to outside servers (Facebook, ShareThis, couter, etc.)  Yet, all of these are static, never-changing.

    How do I set these to have long expiration?  

    Thanks!

    JF 

    1. Hello Josefus,

      Thanks for the question. W3TC Caching plugin tries to set the expiry headers for files on a web page so that they don’t have to be loaded repeatedly. This allows the viewer to see the webpage faster since the file is “cached” and they don’t have to load a new copy of it. If those links are static and don’t change, then you most likely don’t have to worry about it. Also, it is possible that you’re running into files on servers where the expiry header aren’t supported. So, unfortunately, you won’t be able to affect those files with the expiry header. I was looking around the WordPress Codex and found this discussion about Expiry Headers in W3TC. I hope this helps to explain the issue!

      Please let us know if you have any further questions or comments.

      Regards,
      Arnel C.

  14. Thanks! This article is so helpful to me because I was having a problem with WordPress caches, after following this article (of course did some modification with the expiry time) and the problem solved!

    The direct article to my problem wasn’t helpful at all, this solution should be suggested there too! https://www.inmotionhosting.com/support/edu/wordpress/recommended-wordpress-plugins/clearing-cache-in-wordpress

  15. Hello Arnel,

    The configuration which is given by Scott is working. only thing is its not working for the file which is already cached once in the proxy earlier. If i wait till the already defined expiration date expired then after that everything works normal and the modification are relfecting as soon as we change from server end.

    I understood that if the file cached in proxy then we need to clear from prxy end.

    After client accepted to enable this Expire configuration i’ll enalbe in the server and will ask the network team to clear the cache for this specific webserver from their end then everything will start works fine I hope.

    Thank you all for helping me in resolving this issue. 🙂

    Regards,

    Rasheed.

  16. I deleted all the temporary files still i am getting the old files if the same file was accessed once before enabling the expire module.

    If i havent access the file before enabling the expire module then everything works fine.

    Additional information,In my case proxy server is serving the file from the cache

    1. Hello Rasheed,

      Sorry to hear that you’re still having problems with the caching. If you’re using a proxy server that has cached the files, there’s nothing we can clear browser-side. You will need to have the proxy server files cleared. You will also need to check and see if the proxy server will abide by the expiration rules set by the website. Otherwise, the proxy server caching will need to be cleared on a regular basis.

      Regards,
      Arnel C.

  17. <p>Hello Scott,</p>
    <p>So in my case the only soultion is we have to wait till the expire date which is there in the header to expire. After it expired only this expire configuration will work. Am i right ?</p>
    <p>Eventhough clearing the cache in browser wont help ?</p>
    <p>Because i cleared the cache and tested but its not working.</p>
    <p>Please kindly help me in my understanding.</p>

    1. With an external caching server as you are describing, a user would be accessing that server when they access your site in which caching data may not be passed on to the user. To allow browser caching, you would need need to configure this on the external caching server.

    2. Be sure to fully clear all temporary data in your browser. If any references to those files are left over, it will still attempt to deliver the old files.

  18. Hi John,

    Thanks for your response. I was busy with prouction issues and was not able to respond you at the earliest.

    Regarding the theme files (that are Theme ear file) that are not being cached, I reviewed the apache error log and do not notice anything. I need your inputs on below:

    1) Do you think we need to some cache configuration settings on theme customization(as my applicaiton runs on portal).  

    2) We have external caching (Akamai caching), is’t something that blocking my code for mod_expires (ExpireDefault directive type) that I listed earlier.

    Please let me know your thoughts.

    Thanks,

    Prasana 

  19. Hello scott,

     First of all thank you so much your suggestion as it really worked. Now only one problem remains
    Sorry to reply laste as i was struck ip some other work.

    As you suggested i enabled the below in httpd.conf file.

    <FilesMatch “\.(xls)$”>
    ExpiresActive On
    ExpiresDefault A1
    Header append Cache-Control must-revalidate
    </FilesMatch>

    I am telling you here what exactly happening.
    I last modifed a file name sample.xls on August month
    I first accessed the file today before enabling the expire module.
    It gave me the proper file as this is the first time i am accessing.
    Then i modifed the file again today then accessed for 2nd time but it didnt show me the modified content because proxy serves the file from cache.
    From the above steps i replicated the issue, Then i enabled the expire and header module.
    After that i accessed the file again but i was getting the same old file even though i enabled the option.

    I then tried accessing one more file which was ceated on august month first time after enabling the expire. [Difference between above and this one is , In the above case i accessed the file one time before activitating exipre module, but here in this case i accessed the file only after enabling]
    As usual it give the proper content. Then i modifed again and accessed, this time it worked, I can able to see the modified content.

    With the above 2 testing i got the below conclusion.
    If we access an old last modified file for the first time, Heuristic expiration policies set a expire time by deafult.
    After that whatever i access how many time i accessed eventough modifying the file, it will always shows the old file from cache.
    Enabling the expire module also wont work till the expire time set by the Heuristic expiration policies expires. After it expired, a new exipre time wont be set by Heuristic expiration policies as we enabled the expire module.
    If no expire module enabled then again a new expire date is set by the Heuristic expiration policies.

    In the 2nd testing, i didnt access the file before enabling the expire module hence there was no expire time set because of that its working fine eventhough many times we do modifying.

    Your suggestion gave me solution for my current issue, Thank you so much for that.
    But i want to know how to solve the same for the old files also. because its a website where everyone access the file and all the files already accessed by the users. So we cannot go by the 2nd testing result like creating a new file
    We have to make this work for old files also.
    hence could you please tell us what we can do to serve the files not from the cache eventhough already expiration date set by Heuristic expiration policies.

    To simplify my question
    This expire configuration works for the file which is not accessed and cached before in proxy
    What configuration we can do for the file which is already accesed and cached in the proxy and having a expiration date .

    1. Hello Rasheed,

      Browsers should behave the same in this, but they may each act a bit differently. The way it should work, is browsers who have already cached the files will wait until the expiration date set in the headers before checking back. Each user can clear their own cache, but adding the exception for the specific file types will not cause those that have cached already to remove their cached versions.

      Kindest Regards,
      Scott M

  20. Hi, 

    I implemented following mod_expires in our httpd.conf file and run into some issue. 

    LoadModule expires_module modules/mod_expires.so   –> Enabled mod_expires load module on httpd.conf file and added following.

    <IfModule mod_expires.c>

     ExpiresActive on

    <FilesMatch “\.(gif|jpe?g|ico|png|css|js)$”>

      ExpiresDefault “access plus 2 weeks”

     </FilesMatch>

    <FilesMatch “\.(docx?|pff|pdf|xlsx?|zip|tar|gz)$”>

      ExpiresDefault “access plus 1 month”

     </FilesMatch>

    </IfModule>

    While testing, I noticed the jpg files that are published on NAS are being cached properly, example: Internet/FSE_MEDIA/stelprdb5359300.jpg but the files that are on theme directory (application ear file) are not being cached. FSE_WIDTheme/themes/html/FSE_WIDTheme/images/headercorner.gif

    could someone assist on this. Thank You

    – Prasana 

    1. Hello Prasana,

      Thank you for your question. We are happy to help, but it is difficult to troubleshoot since there can be many causes.

      I recommend reviewing your error logs (such as apache error logs) to narrow down the cause, since it can be anything stopping the theme directory from caching.

      If you have any further questions, feel free to post them below.

      Thank you,
      John-Paul

  21. Hi,

    We are having excel files in docroot which will be often updated.

    Whenever we updated the content of the .xls file we are not seeing the modifed content  while accesing through the URL. No script or program we are using. We just putting the file in docroot and access through the URL.

    I feel like its caching somewhere may be in proxy.

    I used mod_header to stop cacheing with below entry in httpd.conf file but still we are not getting the updated content, it takes time to get the updated content. COuld someone help me to fix it.

    <FilesMatch “\.(xls)$”>
    FileETag None
    #<ifModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control “max-age=0, no-cache, no-store, must-revalidate”
    Header set Pragma “no-cache”
    Header set Expires “Wed, 11 Jan 1984 05:00:00 GMT”
    #</ifModule>
    </FilesMatch>

    1. Hello Abdur,
      It sounds as if you simply do not want the xls files to cache. Try using the code below as it should set th xls files to a non caching state.

      <FilesMatch “\.(xls)$”>
      ExpiresActive On
      ExpiresDefault A1
      Header append Cache-Control must-revalidate
      </FilesMatch>

      Kindest Regards,
      Scott M

  22. Hello JacoblMH! Thanks for the answer.
    In my .htaccess file  currently this:

    AddType application/x-httpd-php .php .htm .html

    Options +FollowSymLinks

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www.artchern.com$ [NC]

    RewriteRule ^(.*)$ https://artchern.com/$1 [R=301,L]

     

    RewriteRule ^news_([a-z_0-9]+).html$ news.html?id_news=$1 [L]

    <IfModule mod_expires.c>

    ExpiresActive On

    ExpiresDefault “access 7 days”

    ExpiresByType image/jpeg “access plus 7 day”

    ExpiresByType image/jpg “access plus 7 day”

    ExpiresByType image/png “access plus 6 months”

    ExpiresByType image/gif “access plus 6 months”

    ExpiresByType image/x-icon “access plus 6 months”

    ExpiresByType application/javascript “access plus 6 months”

    ExpiresByType text/css “access plus 6 months”

    ExpiresByType text/javascript “access plus 6 months”

    ExpiresByType text/js “access plus 6 months”

     

    </IfModule>

    and in the file httpd.conf   contains this: 

    RewriteEngine on

    RewriteRule ^/(.*\.)v[0-9.]+\.(css|js|gif|png|jpg)$ /$1$2 [L,E=VERSIONED_FILE:1]

    Header add “Cache-Control” “max-age=604800” env=VERSIONED_FILE

    <FilesMatch “\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|xml|txt|html|htm)$”>

    Header set Cache-Control “max-age=604800, public”

     

    </FilesMatch>

     I wrote above that on a site there is a script “jcart”

      and if absolutely to clean this script “jcart” then the answer of the server

     

       will be correct with the necessary date forward.

    Thanks
    Mike

    1. When viewing your headers, everything looks fine to me and I’m not seeing any dates in the past. It does appear that you may have some server side caching issues that you may want to speak with your server admin about. Unfortunately, I was unable to replicate the issue you are seeing at all.

  23. What is it? Set-Cookie: PHPSESSID=kvtgebg048oc30mvuqnkeiviq2; path=/

    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: max-age=604800, must-revalidate
    Pragma: no-cache

     

    Why the date Expires in past? The server answer with the included script of jcart on a site artchern.com gives such answer lust date, having cleaned this script the server answer at once becomes true and Expires: shows date for a week forward. Any instructions in accesse, httpd don’t give the decision. In the script there are no direct instructions Cache-control (no cache) on a caching ban. well or they are written by any hidden formulas? or it is meant that the POST method programmatically forbids a caching? In what area to look for? Thanks.

    1. Hello Mike, and thanks for your comment.

      The Set-Cookie: PHPSESSID part is simply PHP setting a cookie for the session of a user.

      I’m not sure why your Expires header would be showing a date in the past, what is the code you currently have in your .htaccess file for turning on expires headers? That is not normal behavior, so it seems like it’s either something with your code or your server that is causing this.

      – Jacob

Was this article helpful? Join the conversation!