Disable cache for redirect to mobile version

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

  1. track1

    track1 Well-Known Member

    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 Well-Known 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 )

    $detect = new Mobile_Detect();

    // 3. Check for any mobile device, excluding tablets.
    if ($detect->isMobile() && !$detect->isTablet()) {

    header("Location: http://mobile.domain.com");

    With public cache activated there is no way to make this redirection possible?
    Last edited by a moderator: Oct 19, 2017
  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
    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.
    Last edited by a moderator: Oct 19, 2017
  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.
  6. jirayasanin

    jirayasanin New Member

    Hi guys i have just got the same issue on our website, it caches sometimes mobile version on desktop.
    The developper insistes that the problem is from litespeed itself and proposes that we go back to nginx server.
    we also have Facebook sharing problem on posts, we can't get images or even titles when sharing.
    those two problems we faced only when we migrates from nginx server to litespeed ( thats developper saying ).
    i want some help here please.
  7. NiteWave

    NiteWave Administrator

  8. jalabama

    jalabama New Member

  9. NiteWave

    NiteWave Administrator

    do you know how Mobile_Detect.php detect desktop, tablet and phone ?
  10. jalabama

    jalabama New Member

  11. NiteWave

    NiteWave Administrator

  12. jalabama

    jalabama New Member

    Oh great thanks! I have developed a custom plugin that makes use of the mobile_detect library, so it's easy to adjust the plugin as per your advices. I will read the docs and try your suggestion when time allows it - and likely will post back here.
    NiteWave likes this.

Share This Page