0

I currently Varnish set up for general caching etc, but also acting as a redirect for a mobile version of our website.

It works great (as Varnish does!) and redirects as intended. I decided to add functionality to the VCL config to not just redirect mobiles to the mobile version of the site, but to also redirect desktops accessing a link to the mobile site (for example, on Google) to the desktop version of the site.

However, I can't seem to get this to work in the most puzzling of ways. Here is the VCL:

# Ignoring certain shared assets if (req.url !~ ".(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|css)$") {

     # Let's detect if we're a Mobile
     if (req.http.User-Agent ~ "iP(hone|od)" || req.http.User-Agent ~ "Android" || req.http.User-Agent ~ "Symbian" || req.http.User-Agent ~ "^BlackBerry" || req.http.User-Agent ~ "^SonyEricsson" || req.http.User-Agent ~ "^Nokia" || req.http.User-Agent ~ "^SAMSUNG" || req.http.User-Agent ~ "^LG" || req.http.User-Agent ~ "webOS" || req.http.User-Agent ~ "^PalmSource") {

        # If we're a mobile, set the X-Device header.
        set req.http.X-Device = "mobile";
        # If we've not set a preference to the fullsite to override the redirect, and we're not accessing the mobile site, redirect. This all works fine.
        if ((req.http.Cookie !~ "fullsite")&&(req.url !~ "mobile")){
           error 750 "Moved Temporarily";
        }
     }
     else{
        # We're not mobile. I can see this header is set in the logs.
        set req.http.X-Device = "desktop";
        # If we're a desktop AND accessing the mobile site....
        if (req.url ~ "mobile"){
           # -------------------- THIS NEVER HAPPENS
           error 750 "Moved Temporarily";
        }
     }  
  }

Have a glaring error in the logic here? There aren't any cookies or any other things that might interfere with the redirect that I can see. If anyone has any insight on this, I'd be eternally grateful :) Best regards B

flukeflume
  • 707
  • 1
  • 6
  • 14

1 Answers1

0

Thought I'd revisit incase anyone is reading this with the same problem - We did solve this in the end, and it was due to an incredibly basic oversight.

There was another VCL condition earlier that was interfering with the redirect - a partial match mod ( ~ ) was redirecting ALL matched mobile urls to the desktop version, as this came earlier in the VCL.

It's an obvious point, but my advice to anyone with this problem is to check these partial matches, and remember it'll potentially match any part of the URL.

flukeflume
  • 707
  • 1
  • 6
  • 14