Disable cache for redirect to mobile version

Discussion in 'LiteSpeed Cache' started by track1, May 4, 2013.

  1. track1

    track1 New Member

    Hello,
    im working with litespeed private and public cache with good results, but there is a problem with redirects.

    Using an included mobile-detector script redirect users to mobile version (under different subdomain) or desktop version. But with litespeed cache enabled, it caches redirection, and sometimes shows mobile version to desktop users.

    How can I stop caching this redirection so all users get correct site version?
  2. NiteWave

    NiteWave Administrator

    need more info about "an included mobile-detector script", better provide the source code (can be a conceptual modified version)
  3. track1

    track1 New Member

    All php scripts include code like this to redirect users to mobile subdomain of site, if mobile device detected. ( https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php )

    Code:
    @include_once('Mobile_Detect.php');
    $detect = new Mobile_Detect();
    
    // 3. Check for any mobile device, excluding tablets.
    if ($detect->isMobile() && !$detect->isTablet()) {
    
    header("Location: http://mobile.domain.com");
    exit;
    
    }
    With public cache activated there is no way to make this redirection possible?
  4. NiteWave

    NiteWave Administrator

    wow, the script is super complex :)

    looks difficult or impossible to 100% apply the cache function

    however, there is an idea for your reference:

    set cookie for desktop in php script, for example, desktop=1
    then
    RewriteCond %{HTTP_COOKIE} desktop=1
    RewriteRule .*\.php - [E=Cache-Control:max-age=300]

    while 1st request of a desktop user won't be cached(but set-cookie in browser), the subsequent requests can be cached.
  5. mistwang

    mistwang LiteSpeed Staff

    A simple solution is too add "Cache-control" header along with the "Location" header. so, this response wont be cached, as long as the cache policy is set to respect the response cache-control header.

    However, if one URL could return more than one possible results, it is not public cache friendly, if you turn off caching redirect response, you will find that all user get none-mobile version including the mobile browsers.

Share This Page