Drupal is a registered trademark of Dries Buytaert

Overview

The Override Cache Control Headers module lets you override the Cache-Control HTTP header on a per-page basis. This is useful for bypassing reverse proxy caches or customizing cache behavior for individual pages.

Key Features

- Flexible Cache Control: Override headers for multiple pages for precise caching control.
- Optimized Performance: Tailor cache settings per page to improve site performance.
- Scheduled Header Overrides: Temporarily override HTTP headers for a set duration (e.g., set `Cache-Control: public, no-cache` for 60 minutes, then revert).
- Easy Configuration: Configure via a simple Drupal admin interface and via Drush.

Installation

  1. Download and install the module using Composer.
  2. Enable the module from the Extend page (`/admin/modules`) or via Drush.

Configuration

  1. Navigate to the module's configuration page at /admin/config/develop/override-cache-control-headers.
  2. Enter the URL and the desired `Cache-Control` headers in the provided textarea. Use the `|` symbol to separate the URL from the headers
    • Example: /sitemap.xml|must-revalidate, no-cache, private
    • One entry per line.
  3. The module also provides option to override the cache control headers for the specific time period which configured under Please enter the URL, headers and expiry time in minutes separated by `|`
    • Example: /sitemap.xml|must-revalidate, no-cache, private|10
    • The last parameter specifies the duration, in minutes, for which the custom headers will be applied to the specified URL. Once the form is saved, the module overrides the cache control headers for that URL. After the configured time period expires, the original headers are automatically restored.
    • One entry per line.
  4. The cache control override is also set using the drush command as like below
    drush occh:set-temp-headers "/sitemap.xml|must-revalidate, no-cache, private|10"

Possible Cache-Control Headers

public: Indicates that the response may be cached by any cache.
private: Indicates that the response is intended for a single user and must not be stored by shared caches.
no-cache: Forces caches to submit the request to the origin server for validation before releasing a cached copy.
no-store: The cache should not store anything about the client request or server response.
max-age=: Specifies the maximum amount of time a resource is considered fresh.
s-maxage=: Overrides max-age or the Expires header, but only for shared caches (e.g., proxies).
must-revalidate: Indicates that once a resource becomes stale, caches must not use the response without successfully validating it with the origin server.
proxy-revalidate: Similar to must-revalidate, but only for shared caches (e.g., proxies).
immutable: Indicates that the response body will not change over time.
stale-while-revalidate=: Allows a cache to serve a stale response while it revalidates it in the background.
stale-if-error=: Allows a cache to serve a stale response if the origin server returns an error.

Example Configuration

  • /sitemap.xml|must-revalidate, no-cache, private
  • /contact|no-store
  • /blog|public, max-age=3600
  • /news|must-revalidate, no-cache, private|10

Permissions

The module provides the following permission:

Administer override cache control headers: Allows users to configure the Cache-Control headers for specific URLs.

Usage

After configuring the URLs and headers, the module will automatically apply the specified Cache-Control headers to the matching URLs. The module listens to the `kernel.response` event and modifies the response headers based on the configuration.

Technical Details

  • Configuration form: OverrideCacheControlHeadersSettingsForm
  • Event subscriber: OverrideCacheControlHeaders (listens to kernel.response)
  • Configuration stored in: override_cache_control_headers.settings.yml
  • Drush command: OverrideCacheControlHeadersDrushCommands
  • Provides a hook for custom actions when headers are set (e.g., clearing Varnish/Cloudflare cache):
          /**
            * Hook to allow actions after headers are set.
            *
            * @param array $url
            *   Array of affected URLs.
          */
          function hook_override_cache_control_headers(array $url) {
          }
        

Testing

To run unit tests:

phpunit --group override_cache_control_headers

  • tests/src/Unit/OverrideCacheControlHeadersTest.php
  • tests/src/Unit/UtilityTest.php

Activity

Total releases
8
First release
Mar 2025
Latest release
3 months ago
Release cadence
35 days
Stability
50% stable

Release Timeline

Releases

Version Type Release date
1.0.3 Stable Nov 26, 2025
1.0.2 Stable Nov 21, 2025
1.0.1 Stable Jul 16, 2025
1.0.0 Stable Jul 15, 2025
1.0.0-beta1 Pre-release Jun 3, 2025
1.0.0-alpha2 Pre-release Mar 25, 2025
1.x-dev Dev Mar 25, 2025
1.0.0-alpha1 Pre-release Mar 25, 2025