You can use two major methods to instruct Varnish to redirect a URL: use a module, or edit the .htaccess
file. Acquia Cloud caches 301 redirects in Varnish for a minimum of 15 minutes by default. If you need to change the length of time the 301 is cached, you can use one of the following methods:
1. Ensure that the "cweagans/composer-patches" dependency is present in the composer.json and composer.lock file or else use the following command to install it.
---
composer require cweagans/composer-patches
---
2. Review the composer.json for a section titled "extra" and add the following lines:
---
"composer-exit-on-patch-failure": true,
"patches": {
"drupal/core":
{ "Core 301 redirect cache-control": "https://www.drupal.org/files/issues/2019-12-19/core-301-redirect-cache-control-27.patch" }
---
3. Then install the patch by running the following Composer command:
---
composer install
---
Note
The Redirect module creates the max-age header that is sent to Varnish for redirects. The header is also created later when you enable Allow redirects to be saved into the page cache in the module's settings.
The Redirect module cache settings also apply to other redirects. You can override these on a global or per redirect basis.
Note
Starting with Drupal 7.4 you no longer need to set the $conf['page_cache_invoke_hooks']
variable.
To set Varnish-cachable 301 redirects from .htaccess
, you must set an environment variable using mod_rewrite
and conditionally add caching headers with mod_headers. Because Acquia Cloud already sets a 15 minute cache, this variable is only needed if you want to cache in Varnish for more than 15 minutes.
As an example, this is an .htacce
ss rule to create a 301 redirect from http://example.com/foo/*
to http://example.com/bar/*
that Varnish will cache for two weeks:
RewriteRule ^foo/(.*)$ http://example.com/bar/$1 [R=301,L,E=longcache:1]
Header always set Cache-Control "max-age=1209600" env=longcache
If this content did not answer your questions, try searching or contacting our support team for further assistance.
Wed Oct 22 2025 08:59:29 GMT+0000 (Coordinated Universal Time)