Question

I have a question regarding the image caching and although I am not quite sure, if Magento SE is the right place for this kind of question, but I thought it would be interesting for others to know as well. (If you disagree, I would be glad to know what stack exchange would be a better place for it :-) )

In my shop I am using an image that was usually delivered from another server. It is a seal that can change its content about 1-2 times per day. Unfortunately the other server is not very fast, so I wrote a caching module for it. The module has a cron observer that loads the image once per day and stores it in the media directory. This is ready and working as expected.

My problem is, that the browser does not show the current version of the image. As far as I can tell, Magento sets the image expiration per default as "access plus 1 month" in the .htaccess file in the Magento root directory. This is ok for all images except the one I mentioned.

What is the best way to tell the webserver that this single image has an expiration time of 12 or 24 hours? I would be fine with a solution, that explicitly uses the filename of my image.

Was it helpful?

Solution

The following article makes use of the FilesMatch directive for .htaccess - https://stackoverflow.com/questions/2508783/add-expires-headers-for-specific-images

This seems like the most logical solution. So, you'd end up with something like :

<FilesMatch "^(seal\.jpg)$">
  ExpiresActive on 
  ExpiresDefault "access plus 1 month"
</FilesMatch>

You should be able to use any of the following for timings:

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

OTHER TIPS

#THIS IS WRITTEN TO WORK IN YOUR .HTACCESS FILE
#THIS IS WHAT I GOT TO WORK ON APACHE 2.2 ON 111317 ON A SHARED SERVER AT GODADDY.COM
#PLACED IN YOUR ROOT .HTACCESS THIS APPLYS TO EVERY FILE NAMED SEAL.JPG 
#IF YOU HAVE MANY SEAL.JPG AND YOU WANT IT TO 
#APPLY TO JUST ONE PUT THIS IN THE .HTACCESS FILE IN THE DIRECTORY WHERE
#THE SEAL.JPG FILE IS.
#IF YOU PUT A TIMESTAMP IN THE FILENAME (I.E. sealTIMESTAMP.jpg )
#USE <FilesMatch "^seal.*.jpg$". THE TWO DOTS ARE CORRECT.
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpeg "access plus 1 month"
 <FilesMatch "^seal.jpg$">
  ExpiresByType image/jpeg "access plus 12 hours"
 </FilesMatch>
 <FilesMatch "^seal.*.jpg$">
  ExpiresByType image/jpeg "access plus 12 hours"
 </FilesMatch>   
</IfModule>

**I am 99% certain it does not matter which comes first 
**(ExpiresByType image/jpeg "access plus 1 month") or (<FilesMatch "^seal.jpg$">) 
**FilesMatch will take precedence.
**@Douglas answer set me on the right track,** 
**but I could not get his <FilesMatch "^(seal\.jpg)$"> to work.**
**What did i get wrong?**    
#keywords: how to set Expires HTTP headers on a file named X. 
#change the cache of one file. cache only one file. how to cache one file. 
#I do not want to cache by type Web server cache one file

Not sure on the exact code for less than 1 day, but it would be something along the lines of the below in full that you would need inside your .htaccess file in root:

## 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/x-javascript "access 1 month"
    ExpiresByType application/x-javascript "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

Guessing it is probably simply "access 12 hours" ? or "access 1 day" ?

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top