简体   繁体   中英

How can I prevent images from css file to be cached?

How can I prevent x.jpg from css file to be cached by browser ? These images must be refreshed every day.

CSS file :

#slider{
    background: url(1.jpg) 0 0 no-repeat,
                url(2.jpg) 313px 0 no-repeat,
                url(3.jpg) 626px 0 no-repeat,
                url(4.jpg) 939px 0 no-repeat,
                url(5.jpg) 1252px 0 no-repeat,
                url(6.jpg) 1565px 0 no-repeat,
                url(7.jpg) 1878px 0 no-repeat,
                url(8.jpg) 2191px 0 no-repeat;
}

I tried this in my php file, but I don't know why it doesn't work :

PHP file

<script>
$("#slider").css(
      "background",
      "url(1.jpg?v=<?php echo time(); ?>) 0 0 no-repeat,"      +
      "url(2.jpg?v=<?php echo time(); ?>) 313px 0 no-repeat,"  +
      "url(3.jpg?v=<?php echo time(); ?>) 626px 0 no-repeat,"  +
      "url(4.jpg?v=<?php echo time(); ?>) 939px 0 no-repeat,"  +
      "url(5.jpg?v=<?php echo time(); ?>) 1252px 0 no-repeat," +
      "url(6.jpg?v=<?php echo time(); ?>) 1565px 0 no-repeat," +
      "url(7.jpg?v=<?php echo time(); ?>) 1878px 0 no-repeat," +
      "url(8.jpg?v=<?php echo time(); ?>) 2191px 0 no-repeat"
);
</script>

[Answer of my question]

I added this .htaccess file in my image directory :

.htaccess file

<filesMatch "\.(jpg)$">
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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM