We need to programmatically get the request headers using the mymodule_preprocess_page
hook. To do this we tried the following, however, because we have CDN, Varnish, and Drupal Cache all enabled, the request headers are being pulled from cache so if in the initial request the header value we are looking for is set to false, it is false in all requests afterwards.
How can we get the uncached request headers?
Here is what we tried, but it didn't work.
function mymodule_preprocess_page(&$variables) {
$request = \Drupal::request();
$request_hdrs = $request->headers;
// This is the one that seems to be always caching. (true or false)
// my-header is coming from another server.
$request_custom = $request->get('my-header');
// log this for troubleshooting
\Drupal::logger('mymodule')->log(RfcLogLevel::INFRO, 'header value'.$request_custom);
if($request_custom) {
// based on the header value, redirect to another url.
$response = new Symfony\Component\HttpFoundation\RedirectResponse('www.myurl.com');
$response->send();
return;
}
}