---
title: "Adjusting Caching of Redirects in Varnish"
date: "2023-10-25T05:48:03+00:00"
summary:
image:
type: "article"
url: "/acquia-cloud-platform/help/94451-adjusting-caching-redirects-varnish"
id: "65bd1713-85f7-48ee-9144-8033c9556b36"
---

Table of contents will be added

Introduction
------------

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:

Prerequisites for Drupal 8 and 9 sites
--------------------------------------

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

*   If you need finer control of 301 headers, install the [http\_cache\_control](https://www.drupal.org/project/http_cache_control) module.
*   Although 302 redirects can also be cached, the default Acquia VCL does not cache 302 redirects. Because of this, caching these responses requires a [custom VCL](/node/55873).

Redirect module (Drupal 7) 
---------------------------

The [Redirect](https://drupal.org/project/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.  

![Screenshot of a URL redirects configuration page with options for redirect settings, caching, and deletion preferences.](https://acquia.widen.net/content/212f82f7-5671-4a64-a159-768d2336c828/web/ka06g000001m3Pj00N6g00000VCdgi0EM6g000002WAQi.png)

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.

Redirects using .htaccess
-------------------------

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