Ignoring Varnish based on url

The case: mobile users are getting detected by varnish and hashed to a seperate cache. This allows to use mobile tools in combination with Varnish.
However there is a problem when the mobile visitor (browsing on the m. subdomain) wants to go to the normal (www.) website. If you would simply redirect them to www., they would simply be redirected back to the mobile site. One way to fix this is to disable cache for mobile users who go back to the normal site.

In your varnish .vcl file, in the vcl_recv routine (varnish version 2):

  if (req.url ~ "device=desktop") {
    return (pass);

This disables the cache for every url with ?device=desktop. Now, to keep that url, you want to add it to every other link using this special function which you place in the settings.php of your Drupal (6) site:

function custom_url_rewrite_outbound(&$path, &$options, $original_path) {
  if ($_GET['device'] == 'desktop') {
    $options['query'] = 'device=desktop';

Finaly, you also want some php code to detect if any normal desktop user is trying to use the querystring to circumvent caching, and redirect them or something. I believe this could also be done in VCL.

if ($_SERVER['HTTP_X_DEVICE'] == 'pc') {
  if ($_GET['device'] == 'desktop') {
    // this should not happen


This is the company blog of
Drupal specialist Merge.nl

We are located in Breda (Netherlands) and build websites using Drupal. More about us.

Content on this blog is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.

Creative Commons License

Recent Comments