Sometimes you may want to set specific max-age headers on a page-by-page basis. For example, a website may consist mainly of static pages, but has a news section that updates frequently. It makes sense to cache the static content for a day or more, but to give the news section a brief cache lifetime that's measured in minutes.
Using modules to control the max-age time
These are some modules that can help set max-age headers.
The HTTP Cache Control (D8) module can help you set the cache lifetime for all HTTP Status 404, 302 Redirects and 500 level error pages (when generated by Drupal).
The Context HTTP Headers (D7) module can provide this control. However, this module has a dependency on the Context module, which can add more overhead than you want to your website.
The Cache Control (D7) module provides you with the additional ability to set max-age headers on a page-by-page basis.
The Max age (D7) module allows you to control cache ages per content type.
Changing max-age time for static files
You can change the Varnish cache for static items like files by adding a custom .htaccess
file to any desired folders. It's strongly advised that you do not turn off caching entirely; instead modify the cache to something more appropriate for the website.
For example, placing this single-line .htaccess
file inside a folder will tell external caches (like your browser's cache or Varnish) to cache all files in that directory and any subdirectories for 30 seconds:
# Cache all files on this folder at most for 30 seconds.
Header set Cache-Control "max-age=30, public"
Set this number to 0
to turn off caching entirely in this folder, but this is not recommended. Again, simply create an .htaccess
file in the directory where the files are.
If you want to exclude a file type from Varnish entirely, this example prevents CSV or XML files from being cached.
# Prevent caching of CSV and XML files
<FilesMatch "\.(csv|xml)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>
More information on bypassing the Varnish cache is available in the Bypassing the Varnish cache Help Center article.
D7: Conditionally overriding max-age time for some Drupal paths
You normally set this by using the UI by navigating to Admin > Configuration > Development > Performance on Drupal 7 and entering a value in the Expiration of cached pages field. However, you can override this by setting the $conf['page_cache_maximum_age']
value within the settings.php
file.
For example, the following code snippets change the max age time (in seconds). Use the snippet best suited for your website:
# Set max-age for /path/to/page
if (0 === strpos($_SERVER['REQUEST_URI'], '/path/to/page') {
$conf['page_cache_maximum_age'] = 300;
}
# Using SCRIPT_URL might be better since it ignores query string
if ($_SERVER['SCRIPT_URL'] == '/path/to/page') {
$conf['page_cache_maximum_age'] = 300;
}
# Match all /blog* pages
if (isset($_GET['q']) && strpos($_GET['q'], 'blog') === 0) {
$conf['page_cache_maximum_age'] = 300;
}