Non-zero length redirects in Drupal

Drupal logoThose of you hosting Drupal sites on Acquia might come across this issue: enabling compressed cached pages leads to Varnish errors instead of redirects. As explained by Acquia support, this happens because Varnish tries to unzip zero-length response – and, naturally, then fails. Your options for solutions are to purchase a dedicated balancer configuration – or make the response non-zero (for instance, add the “Redirecting…” text into the body).

Making a response non-zero in Drupal 7 is a bit tricky because the alter hooks are implemented too early in the “goto” routines. You are at risk of adding the response body before sending all the HTTP headers. Moreover, there are many, many implementations of redirect response routines.

In the approach below you are guaranteed to add the body text after all the headers are sent ( hook_exit()  is the last hook to get called). In order to track down only the redirect responses, you can set a static flag in alter hooks – just like in the example below:

Voila!!  🙂