---
title: "Set max-age headers for specific pages, files, or paths"
date: "2025-02-05T23:38:18+00:00"
summary:
image:
type: "article"
url: "/acquia-cloud-platform/help/92841-set-max-age-headers-specific-pages-files-or-paths"
id: "ec8a32e9-3de3-4b51-8083-cb3b46c6a3e9"
---

Table of contents will be added

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](https://www.drupal.org/project/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](http://drupal.org/project/context_http_headers) (D7) module can provide this control. However, this module has a dependency on the [Context](http://drupal.org/project/context) module, which can add more overhead than you want to your website.

The [Cache Control](http://drupal.org/project/cache_control) (D7) module provides you with the additional ability to set max-age headers on a page-by-page basis.

The [Max age](http://drupal.org/project/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](https://support.acquia.com/hc/en-us/articles/360004352373-Varnish-Bypassing-the-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;
    }

Note

This only affects paths which are served by Drupal and are handled by the `settings.php` file.