Disable cache for redirect to mobile version

track1

Well-Known Member
#1
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?
 

NiteWave

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

track1

Well-Known Member
#3
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 )

PHP:
@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?
 
Last edited by a moderator:

NiteWave

Administrator
#4
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
Code:
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:

mistwang

LiteSpeed Staff
#5
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
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.
 

NiteWave

Administrator
#11
#12
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.
 
Top