1

I have a Drupal website where a part of each page varies depending on the value of a certain cookie. In order to use Boost in this situation, I was thinking of rewriting the cookie value into a query parameter, because Boost takes these into acccount for its cache.

To achieve this, I put the following lines in my .htaccess before the Boost code:

RewriteCond %{HTTP_COOKIE} cookie=([a-zA-Z0-9\-]+)
RewriteRule ^(.*)$ $1?cookie=%1 [QSA]

I tested this on a simple PHP script which shows all GET parameters, and the cookie parameter is added based on the cookie. Which is the way it should be. I also tested that these rules actually rewrite the %{QUERY_STRING} for later use, and they seem to do that. However, Boost does not seem to take into account this new query parameter.

Any ideas about what could be wrong? Does Boost do something special with the query string?

Thanks in advance!

1

Ok, it took me a while, but I solved it. It lead me to learn some interesting details about Apache and Drupal.

In my question above I wondered why the rewrite rules did not alter the QUERY_STRING in Apache because apparently, the correct page was not fetched from the cache. However, the problem was not that the correct page was not fetched, but that the correct page was not written to the cache in the first place.

Looking into the code of Boost showed that it uses Drupal's request_uri() function to determine the name of the cache file. This function in turn uses PHP's $_SERVER['REQUEST_URI'] if set. Some tests in PHP then showed that the rewrite rules do actually change $_SERVER['QUERY_STRING'], but that this change is not reflected in $_SERVER['REQUEST_URI']. In other words, the query parameters in $_SERVER['REQUEST_URI'] are the ones of the original request, not the ones as potentially rewritten by mod_rewrite.

To solve this, you can add some code to (for example) Drupal's settings.php to update $_SERVER['REQUEST_URI'] similarly as done in your .htaccess. This worked for me:

if(isset($_SERVER['REQUEST_URI']) && isset($_GET['cookie'])) {
  $cookie = $_GET['cookie'];
  if(strpos($_SERVER['REQUEST_URI'],'?') === false) { 
    $_SERVER['REQUEST_URI'] = $_SERVER['REQUEST_URI'] . '?cookie=' . $cookie;
  } else {
    // I'm not sure what happens if &cookie is already set
    $_SERVER['REQUEST_URI'] = $_SERVER['REQUEST_URI'] . '&cookie=' . $cookie;
  }
}

Note 1: $_GET does contain the rewritten request parameters, so I guess this is based on QUERY_STRING.

Note 2: code above only adds my application-specific cookie. I guess a generic solution would be to completely remove the request string of $_SERVER['REQUEST_URI'] and add all elements in $_GET again.

Note 3: you need both the rewrite rules in .htaccess and this code in settings.php. The former are needed to have Boost fetch the correct page from the cache, the latter to have Boost write the correct page in the cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.