1

I have a site that is a complete redo of an existing one that is ready to go live. I can't find enough information to make a successful transition to the new site.

I have the new site accessible from a sub domain that points to public_dev. In theory I could rename public_html and then rename public_dev to public_html. The problem is that content from the current site will be cached.

Basically I'm looking for a way to ask the browser to not use it's cache if it hasn't been to the site since a specific date.

Should I use .htaccess or header() in PHP or both?

Justin
  • 11
  • 1
  • 3
    http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers – David Mar 27 '12 at 19:04
  • EDIT: Oops, someone beat me to that. This: [Making sure a web page is not cached across all browsers][1] [1]: http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers – Josh Mar 27 '12 at 19:57
  • So should I just disable the cache for a month or so? It seems like that would drastically effect the performance of the site. – Justin Mar 27 '12 at 20:08
  • There are a number of cache related meta tags that can be used to control this but remember that you need to set those on the current system first for them to have any effect. – John Gardeniers May 08 '12 at 02:56

3 Answers3

2

Why not take a belt and braced approach and use both. You could also set the meta tags in your content head section (using an include file for site wide updates)

<META HTTP-EQUIV="Cache-Control" CONTENT="max-age=0">
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
1

If you are serving your site from straight up PHP files, then you probably won't have any caching issues (unless you have implemented some form of cache management). However, things like stylesheets, images, and other files could still be cached by the browser.

Your best bet is probably to control your cache via .htaccess and set everything to expire on a day before you intend to roll out your website: .htaccess - How to set expires to date in past?.

AndrewX192
  • 11
  • 1
  • 2
    Output generated by PHP is indistinguishable from that generated by HTML files, so it has absolutely no effect in determining whether that content is cached or not. Remember, the browser sees only the output, not how it was generated. – John Gardeniers May 08 '12 at 02:55
  • Correct, but I have seen browsers cache .html files based on the Last-Modified date, that did not cache the same content when it was placed in a PHP file (due to lack of Last-Modified). – AndrewX192 May 08 '12 at 06:19
1

The only sure way to invalidate all caches simultaneously is to change the URLs that the browser actually sees. So your http://example.com/index.php becomes something like http://example.com/2/index.php. All static files also need to have the new URL path.

This is why a Google and others build an "asset rename" process into their code for all static assets based on the MD5 hash. That way you can set cache control to 10 years, knowing for sure that any requests for /03b/c49/ef94589d54390435ab0943cd212.png will always return the exact content intended, and that URL will never be requested when it is replaced by "newer" assets. But that takes a lot of work to set up, so using relative URLS with a new root path as mentioned above is more common.

rmalayter
  • 3,762
  • 20
  • 28