Search code examples
phpmobiledrupal-6http-redirect

redirect users from desktop site to mobile site based on URL


I've got a mixed Drupal6 and php redirect question that I really need help with.

I've got a Drupal 6 site, with a smaller mobile site on a subdomain. I'm building a custom module to redirect visitors of the site to the mobile site based on device and location (US users only).

I'm using mobile detect to discover the user's deviceL: https://github.com/serbanghita/Mobile-Detect

and this answer to get my absolute URL on my drupal site: How to get the full URL of a Drupal page?

So far both pieces are working fine, but I'm not sure how to write the lines to go from one location to the next. I've got about 8 specific urls that need to be redirected that would look something like this:

http://desktopsite.com/specific-page -> http://m.mobilesite.com/specific-page

I'm sure it's a basic PHP thing, but I'm stumped and could really use some guidance so I can move in the right direction. I've also tried contributed drupal modules, but because I have multi-languages sites I can't use them (they don't support the languages)


Solution

  • I think I understand what you are saying:

    // Make a mobile detect object
    $detect = new Mobile_Detect();
    // Check the device
    if($detect->isMobile()){
        // Here is where you put the page you have 
        // i.e. http://desktopsite.com/specific-page;
        $location = "http://desktopsite.com/specific-page;";
        // We need to swap the url in that $location variable
        $location = str_replace("http://desktopsite","http://m.mobilesite",$location);
        // Then to a classic redirect
        header("Location: {$location}");
    }