Redirect mobile devices to alternate URL with Varnish 3.0 and Drupal 6

This is a short note for ourselves, but it might help others considering the (new for now) rules of Google indexing of non-mobile sites. Drupal 6 does not really provide all the stuff that would be really useful to switch the theme of your site depending on the browser being on a mobile device or not. There are a few modules that should help you, like switchtheme, and mobile_subdomain, but in the end, you can also do it as follows.

Setting up Apache (or web server x)

Let's assume your site is called and you want mobile devices to see another theme. Define the subdomain in your DNS configuration and point it to the same server. In your web server configuration, add an alias for that second URL.


Just add the new subdomain as a ServerAlias directive:


Just add the new subdomain after "server_name", like so:

Setting up Drupal 6

Build a small module yourself (based on Switchtheme) with just what you need, and a mobile theme (in this case called mymobiletheme). I won't say anything about the theme itself. You can test it in admin mode if you want. Just make sure it works and it's not dependent on the URL or something like that. Let's say your module is called mobile_yoursite. You would have just two files:
// mobile_yoursite/ file
name = Yoursite mobile theme switcher
description = Changes the theme depending on the calling URL
package = Yoursite
core = 6.x
; Information added by packaging script on 2013-03-26
version = "6.x-1.0"
core = "6.x"
project = "mobile_yoursite"
datestamp = "1364280012"
// mobile_yoursite/mobile_yoursite.module file
 * Switch theme to Garland when request comes on
function mobile_yoursite_init() {
  global $custom_theme;
  if ($_SERVER['HTTP_HOST'] == '' || $_SERVER['SERVER_NAME'] == '') {
    $custom_theme = 'mymobiletheme';
Copy the module inside the sites/all/modules/ folder (or sites/default/modules if you prefer) and enable it. Now if you load directly, you should see the site with the mobile theme. If you have issues refreshing and getting on the wrong site, make sure the configuration of caching does NOT include CSS and JS optimization, as this is sometimes a cause of trouble for themes switching.

Setting up Varnish

This only works with Varnish 3+, so if you use an older version... sorry. Go to your Varnish configuration directory (where your default.vcl is). Download this file: right into that folder. Edit your default.vcl file: # At the very beginning include "mobile_detect.vcl"; # Inside vcl_recv if ( ~ "^your\.site\.com$") { call devicedetect; if (req.http.X-UA-Device ~ "^mobile") { error 750 "Moved Temporarily"; } } # Inside vcl_error if (obj.status == 750) { set obj.http.Location = "" + req.url; set = ""; set obj.status = 302; return(deliver); } Now reload your Varnish configuration (you can test your config with "varnishd -Cf /etc/varnish/default.vcl" if you like. Now load your page through a mobile device... you should be automatically redirected to