简体   繁体   中英

PHP cache header override

I've been through over 100 answers here, lots to try, NOTHING working??

Have a PHP based site. I need caching OFF for all .php files EXCEPT A SELECT FEW.

So, in .htaccess, I have the following:

ExpiresActive On
# Eliminate caching for certain dynamic files
<FilesMatch "\.(php|cgi|pl)$">
ExpiresDefault A0
Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

Using Firebug, I see the following:

Cache-Control   no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform
Connection  Keep-Alive
Content-Type    text/html
Date    Sun, 02 Sep 2012 19:22:27 GMT
Expires Sun, 02 Sep 2012 19:22:27 GMT
Keep-Alive  timeout=3, max=100
Pragma  no-cache
Server  Apache
Transfer-Encoding   chunked
X-Powered-By    PHP/5.2.17

Hey, Looks great!

BUT, I have a couple .php pages I need some very short caching on.

I thought the simple answer was having this added to the very top of each php page in which I want caching enabled:

<?php header("Cache-Control: max-age=360"); ?>

Nope.

Then I tried various versions of the above. Nope.

Then I tried meta http-equiv variations. Nope.

Then I tried variations of the .htaccess code along with the above variations, such as limiting it to:

# Eliminate caching for certain dynamic files
<FilesMatch "\.(php|cgi|pl)$">
Header set Cache-Control "no-cache, max-age=0"
</FilesMatch>

Nope.

It seems nothing I do will allow a single .php to be cache enabled with the .htaccess code in place, short of removing the statements from the .htaccess file altogether.

Where am I going wrong? What do I have to do to get individual php pages to be cacheable while the rest remain off??

Thank you for any thoughts.

Well, apparently this has no answer. So, my solution at this point is to eliminate the .htaccess code altogether, and apply explicit headers to each file. A pain in the you-know-what but it's time to move on. If anyone has a more elegant solution that can work with an .htaccess default please feel free to share... thanks

So I know I'm late.. maybe too late. but I came across a simular problem, and I thought I'd share my solution.

Basically, I turned ExpiresActive Off for every file I didn't want to be cached (or have different caching times than my static resources). It looked something like this:

ExpiresActive On

<FilesMatch "\.(php|cgi|pl)$">

  # This makes sure that no cache headers can be set,
  # but does not generate an error when trying to.
  ExpiresActive Off

</FilesMatch>

# Your normal caching here
ExpiresDefault "access plus 1 month"

Now over in your PHP script, you should be able to insert caching headers without them being overwritten by your .htaccess file, just like you were doing

<?php header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 360) . ' GMT'); ?>

Hope this was helpfull.

This answer had the solution for me: https://stackoverflow.com/a/4521120/2685496

Little did I know that session_start(); by default overwrites your Cache-control and Expires headers with values to ensure the page is not cached.

You can use session_cache_limiter('public'); before session_start(); , like Marcin suggests, or you can just put your header(); statements after session_start(); .

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