I’m trying to set up caching on my Apache site starting with a basic configuration that I can test a PHP script with a browser:
CacheEnable disk /
However, it only works if the page is not “redirected” in some way. For example, I’m testing this simple PHP script in the root directory called “test.php”:
If I go to “…/test.php” in my browser, it works: the output time doesn’t change after the first visit.
If I go to “…/test”, it doesn’t work: the output always changes to the current time.
When it works, I see an “age” field in the cached response with an updated number of seconds since the page was first served. When it doesn’t work, there is no “age” field in the response.
I don’t have an .htaccess file in the root directory, and I haven’t been able to find where Apache knows to serve the script without the “php” extension. When I test using an .htaccess file with a RewriteRule, it doesn’t cache the response when it matches. Not sure if this matters.
Typically you need to set your caching headers correctly for page caching to work. There’s a good article here, and Apache docs here.
In summary of that article, you want to set the Cache-Control header. You can set etag, but the rest like pragma you shouldn’t bother with.
In Apache this is done with mod_expires, which would be configured something like this.
# enable expirations
# expire GIF images after a month in the client's cache
ExpiresByType image/gif A2592000
# HTML documents are good for a week from the
# time they were changed
ExpiresByType text/html M604800