Something useful for other modules that we currently have to hack around is allowing 301 redirects done with drupal_goto() to be cached in {cache_page}. Basically allowing $options['cache'] as a parameter in drupal_goto() that would call drupal_page_footer() rather than drupal_exit().

Comments

Crell’s picture

Once we're converted to using HttpCache instead of the current page cache, it should properly cache all responses. A request should never terminate early for any reason, ever, other than a bug. See #1597696: Consider whether HttpCache offers any significant benefit over the existing page cache

deviantintegral’s picture

Issue summary: View changes
StatusFileSize
new778 bytes

Here's a simple patch for D7 for anyone wanting to ensure 301's are always cached. This requires that page caching be enabled, and uses exactly the same expiry rules as other responses.

dave reid’s picture

Status: Active » Needs work

@deviantintegral: You'll want to add the changes from #1392974: cache control headers are not correctly added to redirects as well to this patch. Basically,

      if ($cache = drupal_page_set_cache()) {
        // When caching this redirect for the first time we still need to ensure
        // that the correct cache headers are sent.
        // @see drupal_page_footer()
        drupal_serve_page_from_cache($cache);
      }
e0ipso’s picture

StatusFileSize
new1.02 KB

I've modified @deviantintegral's patch above. I had to add the Location header manually to the headers array since it was not added through drupal_add_http_header due to the custom http response code. I also manually added the fake status header to allow the status code to be cached as well.

dawehner’s picture

Version: 8.x-dev » 7.x-dev

D8 already allows you to set the http status code, so you can do pretty much all you want. Moved to d7.

saitanay’s picture

StatusFileSize
new1.17 KB

Allows caching for both 301 and 302

This could be used in conjunction with redirect module that gives an option to cache redirects.

dcam’s picture

Status: Needs work » Needs review

Activating Testbot.

The last submitted patch, 4: 1678348.4-cache-redirect-drupal-goto.patch, failed testing.

dave reid’s picture

Status: Needs review » Needs work

This has one major problem:

  1. User requests `/My-Page` with a completely cold cache
  2. Global Redirect module says to redirect to /my-page instead with the "Case Sensitive URL Checking" enabled by default.
  3. Drupal core [(patched with https://www.drupal.org/node/1678348)](https://www.drupal.org/node/1678348) includes headers that allow this redirect to be cached
  4. Drupal's page cache stores the request for `/My-Page` with the header `Location: /my-page`
  5. User requests `/my-page` with a now-warm page cache (because MySQL is case-insensitive by default for VARCHAR columns, like cache_page.cid)
  6. Drupal's page cache response includes header to redirect the current request to /my-page, which creates an infinite redirect loop.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.