This is more of a "how to" since it wasn't immediately clear how to accomplish this with Boost.

Install Boost as normal.

Add the following code to settings.php:

  // Swap Boost's cache path based on what the .htaccess sets.
  if (!empty($_SERVER['boostpath']) && 'normal' !== $_SERVER['boostpath']) {

      $conf['boost_normal_dir'] = $_SERVER['boostpath'];
  }

In your .htaccess file look for RewriteRule .* - [E=boostpath:normal]

Above that line add additional RewriteRules that are applicable for your site. On my current project those are the following:

  # Allow for alt paths to be set via htaccess rules; allows for cached variants (future mobile support)
  # Change path for mobile devices
  RewriteCond %{QUERY_STRING} ^device=1$ [OR]
  RewriteCond %{HTTP_COOKIE} device=1
  RewriteRule .* - [E=boostpath:mobile]
  # Change path for tablets
  RewriteCond %{QUERY_STRING} ^device=2$ [OR]
  RewriteCond %{HTTP_COOKIE} device=2
  RewriteRule .* - [E=boostpath:tablet]
  # Default for other
  RewriteCond %{QUERY_STRING} !^device=(1|2)$
  RewriteCond %{HTTP_COOKIE} !device=(1|2)
  RewriteRule .* - [E=boostpath:normal]

Slightly below the previous bit of code will be a chain of RewriteCond rules. The comment above them is # Skip boost IF not get request OR uri has wrong dir OR cookie is set OR request came from this server OR https request

With these changes Boost will make use of the boostpath defined and place cache into a normal, tablet, or mobile directory depending on device type.

Again, these rules are not generic -- they are specific to the way you're doing mobile detection. For this project I am using Context Mobile Detect. If you are using that module then these rules are drop-in. If you are using another method you will need to write your own.

Comments

ryanrain’s picture

Thanks Steven for this "how to," it started me off in the right direction.

The addition you suggest to settings.php worked perfectly, however my modifications to my .htaccess file were a little different. Here's what i have working on my site:

  # Default "normal" environment variable specifies that the desktop Boost cache should be served.
  RewriteCond %{REQUEST_URI} !^device=(1|2)$ [OR]
  RewriteCond %{HTTP_COOKIE} !device=(1|2)
  RewriteRule .* - [E=boostpath:normal]
  
  # Use various methods to check whether the user is on a mobile device in order to serve the mobile version Boost cache.
  # Query string and Cookie from context_mobile_detect module
  RewriteCond %{REQUEST_URI} ^device=1$ [OR]
  RewriteCond %{HTTP_COOKIE} device=1 [OR]
  # Since REQUEST_URI appears to be processed before Drupal is initiated, we need to also check here.
  RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
  RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
  RewriteCond %{HTTP:Profile}       !^$
  RewriteRule .* - [E=boostpath:mobile]

A few notes on my changes:

In looking at the environment variables, I saw that Context Mobile Detect adds the device and device type to the REQUEST_URI. QUERY_STRING remains empty. Maybe I'm misunderstanding the distinction between these two, but I found that this change made my code start working.

I also couldn't get the correct boost cache served to anonymous visitors who are visiting the site for the first time until i added RewriteCond that do a rudimentary check whether the user is on a mobile device. i assume this is because the rewrite rule would operate before drupal is even bootstrapped. i ended up with some significant redundancy which i don't like, but couldn't figure out a way to use the php DeviceDetect function from within .htaccess.

another quick and easy version of this code, which would eliminate redundancy and the dependency on the Context Mobile Detect module would be the following:

in .htaccess:

  # Default "normal" environment variable specifies that the desktop Boost cache should be served.
  RewriteRule .* - [E=boostpath:normal]
  
  # Use various methods to check whether the user is on a mobile device in order to serve the mobile version Boost cache.
  RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
  RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
  RewriteCond %{HTTP:Profile}       !^$
  RewriteRule .* - [E=boostpath:mobile]

and in settings.php

use the exact same code suggested in steven's post above.

hope this helps!

steven.wichers’s picture

I also couldn't get the correct boost cache served to anonymous visitors who are visiting the site for the first time...

This is why I was checking the query string. The query string device parameter is added by Context Mobile Detect. On the first page hit the user (most likely) does not have it set, so they get a non-boost page. CMD then flags them as mobile, tablet, etc. and they will get the correct boost cache from then on. If you have a "View on desktop" style link then it needs to specifically set the device query string.

Anonymous’s picture

This is very commendable, what concerns me is that the new drupal 8 context specific content, boost and responsive themes has not been road mapped (not to mention the symfony caching system). My personal opinion would be that a good responsive theme would use css rather than server side processing to deal with differing formatted devices and since boost only caches the outputted page, then the css could be dynamic (if really necessary) so it would still function as it always has.

I'm getting a little concerned that drupal has moved away from MVC to a solution that could impact the overall performance as now for each page one if going to have 3 database hits (e.g. serving the mobile, tablet and phone devices), building the caches, 3 times the work for cron when they'd gone one way with Bartik in 7.x

Anonymous’s picture

Issue summary: View changes

Removed rules that were skipping the newly created cache

DavidPetit’s picture

I have a sandbox project which do almost the same thing:
https://drupal.org/sandbox/davidpetit/2153807

The purpose is to have two versions of the same site with same domain, while still using boost depending on user agent (mobile/desktop).
We serve a different Drupal theme depending on the user agent (for mobile version).

newswatch’s picture

This is for D7. Is it possible to do the same thing in D6?

Thanks!

~ Subir

santiwww’s picture

I am using Boost and also Context Mobile Detect, made the modifications in .htaccess and settings.php and i get the cached page when using "normal", but anytime i use mobile the cache file is generated again, so the caching mechanism is worthless, every request generates a file and overwrite the previously created one.
Is there any way to make the caching working in mobile as well?