You can configure specific max-age headers for individual pages based on your requirements
To configure max-age headers for specific pages, install and configure the following modules:
You can modify the Varnish cache for static items such as files, add a custom .htaccess file to the target folders
For example, when you add a single-line .htaccess file to a folder, external caches such as your browser cache or Varnish 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 value to 0 to disable caching in this directory.htaccess file in the directory that contains the files.
To exclude a file type from Varnish, use the following example to prevent caching for CSV or XML files:
# 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>Cloud Platform Varnish configuration does not automatically process JavaScript (.js) files and modern ECMAScript (ES) module JavaScript (.mjs) files in the same manner.mjs files, you must explicitly include the extension in the <FilesMatch> pattern:
<FilesMatch "\.(js|mjs)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>For details on how Varnish handles .mjs files, refer to Static files in Varnish.
For more information on bypassing the Varnish cache, refer to Bypassing the Varnish cache.
The <FilesMatch> and <Files> directives match only against the file name portion of a request and not the full URL path
Apache provides two directives for file name matching
<Files "name.js"> : Matches an exact file name or a simple wildcard, such as *.js<FilesMatch "\.js$"> : Matches a file name by using a full regular expression.mjs and .js files with \.(m?js)$.Neither directive matches against a URL path.
Place a separate .htaccess file directly in the target directory, for example, themes/common-assets/pdf-viewer/.htaccess)
# themes/common-assets/pdf-viewer/.htaccess
Header set Cache-Control "max-age=604800, public"<If> blocks with %{REQUEST_URI}Cloud Platform supports Apache 2.4 and later, which allows conditional blocks through the <If> directive to test against the full request Uniform Resource Identifier (URI)
<If "%{REQUEST_URI} =~ m|^/themes/common-assets/pdf-viewer/|">
Header set Cache-Control "max-age=604800, public"
</If>mod_rewrite conditions with an environment variableUse RewriteCond to test the request URI and set an environment variable, then apply the header when that variable exists
RewriteCond %{REQUEST_URI} ^/themes/common-assets/pdf-viewer/
RewriteRule ^ - [E=CACHE_LONG:1]
Header set Cache-Control "max-age=604800, public" env=CACHE_LONGDo not use <Files "/themes/common-assets/pdf-viewer/*"> to target a directory path/themes/common-assets/pdf-viewer/* literally<Location> or <LocationMatch> in .htaccess files because Apache ignores these server-configuration directives without warning
The <Location> and <LocationMatch> directives match against URL paths and appear useful for path-based cache rules.htaccess files without warning<If> or mod_rewrite methods described previously.
mod_expires to set cache lifetimesCloud Platform supports the Apache mod_expires module to set cache lifetimesCache-Control header value, you can use mod_expires to define expiration times relative to the last access or modification time of a file.
Example 1: Cache all files for two weeks after access:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A1209600
</IfModule>Example 2: Target specific MIME types:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/css "access plus 1 week"
</IfModule>To choose between the two modules
Use mod_expires to set expiration times relative to access or modification time by using human-readable syntax
Use mod_headers with Header set Cache-Control when you require explicit control over the full Cache-Control header value, such as adding no-store, must-revalidate, or s-maxage
If both modules set Cache-Control, mod_headers takes precedence
To configure page caching in Drupal 7, navigate to Admin > Configuration > Development > Performance, and enter a value in the Expiration of cached pages field$conf['page_cache_maximum_age'] value in the settings.php file
For example, the following code snippets update the maximum cache lifetime in seconds
# 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;
}This setting affects only the paths that Drupal serves and the settings.php file handles