when I am debugging to ensure that some files are not cached and therefore reloaded anew each time, I frequently link files with something like
<script src="script/js.js?<?=time()?>"></script>
Is there some way I can do something in css to ensure background images are reloaded every time the page loads without making the style something other than a .css file? (php with css header for example)
#bg_div{
background: url(../images/darrow.png.....)
}
Thanks!
You could use .htaccess like this:
<filesMatch "\.(gif|jpg|png)$">
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 "Mon, 26 Jul 1990 05:00:00 GMT"
</ifModule>
</filesMatch>
This prevents all images to be cached. If you put the .htaccess file in a specific folder then images in this folder doesn't get cached.
Note that on sites with heavy load cache control should be carefully planned. Using a htaccess directive like this can VASTLY affect page load times.
Related
I have a Wordpress site running a plugin called OpenHours. Basically, we program in our operating hours to the backend of the site and then have a page /hours/ which shows them. I have disabled every possible Caching feature in Wordpress and tried editing the .htaccess file with the below:
# DISABLE CACHING
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$">
<IfModule mod_expires.c>
ExpiresActive Off
</IfModule>
<IfModule mod_headers.c>
FileETag None
Header unset ETag
Header unset Pragma
Header unset Cache-Control
Header unset Last-Modified
Header set Pragma "no-cache"
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
</IfModule>
</FilesMatch>
We have a mobile app that basically links users to the webpage through their phone's browser, but anytime the link is clicked the page doesn't update (The plugin is configured to show "Currently Open" or "Currently Closed" based on time of day). Is there a way to force the page to update and not cache whenever it is visited?
I tried a query string but unfortunately the spot we insert the link in the app does not support any type of coding, just a straight link, so /hours/?rnd="+Math.random() and /hours/?rnd="+new Date().getTime() haven't worked.
You can set new headers for a specific page using the default PHP header function.
add_action("init", function() {
// TODO: Replace with the actual page ID or title
if(!is_page("page-without-cache")) return;
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
}, 10);
We used the init function to make sure that the header is sent before rendering the page.
And if you are using any cache plugin, you should be able to create a list of URLs or directories that you do not want to include or that you do not want to be cached. For example, in the WP Super Cache plugin, the option is located in the Advanced tab > Accepted Filenames & Rejected URIs section. By default, these paths are excluded from cache.
wp-.*\.php
index\.php
And in addition, WP have dedicated functions called nocache_headers or wp_nocache_headers, which you can also use to prevent caching. The functions are also using the default PHP header function.
I have a php script which copies an image to a folder every hour over writing the last image, the name of the image is always the same, image.jpg , im using ifttt to post the image to a website but the image posted is always the same image even though I have checked to see the image is different. so it must be posting the image from the cache? I added clearstatcache(); to the code but it doesn't work. is there any other way of clearing the cache so the actual image is posted? the image is hosted on a shared server. Thanks
$from = '/A1';
$files = scandir($from);
$to = '/A2';
if (!empty($files[2])) {
rename("{$from}/{$files[2]}", "{$to}/image.jpg");
}
clearstatcache();
solved with .htaccess file
<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$">
<IfModule mod_expires.c>
ExpiresActive Off
</IfModule>
<IfModule mod_headers.c>
FileETag None
Header unset ETag
Header unset Pragma
Header unset Cache-Control
Header unset Last-Modified
Header set Pragma "no-cache"
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
</IfModule>
</FilesMatch>
Thanks for all your help!
I changed the .htaccess file to cache everything for a year.
After a while, I noticed that I don't see the updates I make to the website.
I figured out, that because I changed the default cache to a year, somehow the html file got cached for 1 year, and even though I've changed it back, still doesn't work.
The only way I could make it update is to use different links (like www.domain.com/newlink insead of www.domain.com/oldlink), but that doesn't repair my frontpage, as it is www.domain.com, and I can't change that.
Do you have any solution for my problem?
P.S. It isn't my browsers cache, as I have cleared it's cache, I even reinstalled it, and if that's not enough, I can't see the updated version on my phone.
P.P.S. I can see that the http request doesn't even reach the server for the www.domain.com link.
P.P.P.S. I tried using a proxy, and the page worked as it should, so I think it's only cached at my ISP.
Can you try to explicit say not to cache to apache in the htaccess. Also restarting your apache server might work.
<filesMatch "\.(html|htm|js|css)$">
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>
I have a mini costum framework. All URLs are made on the index.php?op=ControllerName&Action=Methode&elemID=211 for example.
I generate the friendly URL of all specific URLs.
The problem is that I want to disable cache using .htaccess on all generated URLs.
So for elem 211 or 215 or 233 etc... I want that the server is getting the page not from cache.
Every time that the index.php is called I want that the browser use cache only for CSS, JavaScript, images and not for mysite.com/211-friendly_URL or mysite.com/215-friendly_URL_number_2
I used this but it didn't work
<ifmodule mod_headers.c>
<Files index.php>
FileETag None
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"
</Files>
</ifmodule>
thanks for your help;
I HAve a Website http://fbtricks.cf/
And It's Hosted in Hostinger.in
M Using PHP Extension, Because M Using a Common Header and Footer For All Files.
The Problem Is, I Have Made Changes to The Header and Footer. But Still The CHANGE is Not Reflecting in All Files. :(
And It's Not Just an ISsue in My Browser, But in Every Browser, The Old Header and Footer is Displaying Except in The Index File.
I THINK THAT, MAYBE IT"S HAPPENING BECAUSE THE SERVER HAS CACHED THE FILES.
Plz Provide Me With a Solution, as It's a Serious Issue For Me and My Website and Also For Website Visitors.
header file Link - http://fbtricks.cf/header.php
I'm not sure if this .htaccess file can override the global settings in Hostinger.
.htaccess:
<filesMatch "\.(html|htm|js|css)$">
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>