28

I have a website that because of an ill-prepared apache conf file has instructed users to cache a website URL several years into the future. As a result, when a person visits the site, they often make no attempt to even request the page. The browser just loads the HTML from cache.

This website is about to get a major update, and I would like for users to be able to see it. Is there a way for me to force a user to actually re-request the webpage? I fear that for some users, unless they happen to press F5, they may see the old webpage for several years.

Chris
  • 746
  • 4
  • 10
  • 15

9 Answers9

18

Use different URLs. If the main entry point to your website (like the main index file) is cached, then you're screwed... maybe you should register another domain name?

liori
  • 40,917
  • 13
  • 78
  • 105
  • 4
    The site ranks high in search engines; A different url would not work in our case base of the SEO penalty – Chris Jul 30 '09 at 16:38
  • 13
    You can make it a different URL by adding a URL parameter, like a timestamp: ?new=. This would not screw up the SEO. – Julien Mar 08 '10 at 17:48
5

There are several options to achieve this First In the section add meta tag:

<meta http-equiv="pragma" content="no-cache" />

Basically the browser won't cache the page.

Another option is to set sessions, this will force the browser to a new session each time they go, and thus make the browser get the page from the server instead of the cache

<?php
session_start();

$_SESSION = array();
session_destroy();

?> 

You can add this to your website for a couple of days and then remove. I really don't know if it will do, but perhaps you will find it useful

Sorantis
  • 14,496
  • 5
  • 31
  • 37
  • 18
    If the browser has the page in cache, it'll never hit the server, so this won't help at all. – ahockley Jul 30 '09 at 16:11
  • 1
    Yeah, I just gave this a try and it didn't work. Thanks for the idea though. – Chris Jul 30 '09 at 16:36
  • Drat - I thought you had it. I have a file http://www.sdsolarblog.com/montage/show.html that changes all the time, to show the latest file with a header. (Right now the latest file is http://www.sdsolarblog.com/2017-10-15.jpg) It keeps showing me yesterday's version even when I clear Chrome's cache. – SDsolar Oct 16 '17 at 02:57
4

It is arguable that if your "major update" is just in a few (2 or 3) weeks, you only need to reconfigure your apache conf now (no far future stuff for html - only for assets and content that will most likely never change). The Firefox cache is ~50MB by default and that is not much because images get also cached and modern websites have a lot of content.

Not perfect - but thats what I would do - when I don't want to or can't change the URL's ;)

meawoppl
  • 2,714
  • 1
  • 23
  • 30
biophonc
  • 414
  • 1
  • 4
  • 8
  • This is what I hope will happen. I updated the the apache conf a few weeks back to remove the caching. It seems there is really no way to force the refresh. – Chris Aug 01 '09 at 12:38
3

I realize this question is very old, but I have a viable answer:

Of course you can force new URL's to avoid common caches (not long term ones)...

I.e.

  • Add query to static .js/.css/.html files
  • Add meta pragma tag for no-cache
  • Server-side redirections, no cache, eliminate cookies, sessions, etc.

However in a scenario like this (formerly edited apache .conf for long time caching) since you cannot change the domain for SEO purposes, there is a 'crude hack' you can use which will minimize the impact to SEO:

Copy your index page (i.e. index.php) to a new page (i.e. index_new.php) and edit httpd.conf (or equivalent) so that the DirectoryIndex is the new page. Then just delete or move your old page, it should theoretically always redirect to the new page.

I.e. in Debian/Ubuntu:

cd /var/www
cp index.php index_new.php
sudo vim /etc/apache2/sites-enabled/000-default

    <Directory /var/www/>
        ...
        DirectoryIndex index_new.php
    </Directory>

mv index.php index_old.php
sudo service apache2 restart

And there you go.

3

Years late, but it might help someone down the road: if you've got any javascript file that isn't cached years into the future (i.e. if you have any way of running new js on the cached site), add some js that will programatically clear the cache. Once the configuration is fixed and/or the update complete, remove the cache clearing js.

Ctrl + F5 in jquery to clear browser cache

Community
  • 1
  • 1
alexanderbird
  • 3,847
  • 1
  • 26
  • 35
3

You can make all the necessary changes inside the HTML files, like

<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0" />
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

But also, you can explicitely tell Apache to use mod_expires.c module, and add a couple of directives to httpd.conf file:

<IfModule mod_expires.c>
  # Turn on the module.
  ExpiresActive on
  # Set the default expiry times.
  ExpiresByType text/html "modification plus 5 seconds"
  ExpiresByType text/javascript "modification plus 5 seconds"
  ExpiresDefault "access plus 2 days"
</IfModule>

This way you add the http headers cache-control and expires, to responses, in order for thw browser to update the cache 5 seconds after the file was modified in the origin, for those kinds of files, and 2 days after being accessed by the browser for all the other kinds of files.

Hope this helps.

Camilo
  • 439
  • 5
  • 13
2

I think that there is no way of doing this. If they never contact your sever there really is nothing you can do about it.

Jack Ryan
  • 8,396
  • 4
  • 37
  • 76
2

The browsers are using the cache for optimizing calls to the servers, in the case of any update of your build production you have to force browser to refresh resources from the server just like use f5 to clear the cache.

The solution for that you should add some parameters to your meta-includes (URLs) by the way the browser will not find the resources in the cache with such parameter so it will refresh them from the server.

You can parameter your includes per version for example like the below :

<script src="assets/js/customscript.js?v=1.1"></script>
<link rel="stylesheet" type="text/css" href="assets/style/main.css?v=1.1"></link>
Brahim SLIMANI
  • 310
  • 1
  • 8
1

change URLs of every resource

Alex. S.
  • 143,260
  • 19
  • 55
  • 62