1

I am using the following code in a module for redirection to a subdomain whenever a mobile is detected;

<?php

  function mobydetect_init() {
  include("PATH/TO/Mobile_Detect.php");

  $detect = new Mobile_Detect();
  if ($detect->isMobile()) {
    header('Location: http://m.mysite.com');
    drupal_exit();
  }
}

However when I go to my site using a mobile I get this error message;

"This web page has a redirect loop."

I have disabled to subdomain from redirecting to the main domain in Aegir. So where's the loop coming from?

1
  • have you tried using chrome / safari web tools to inspect the browser traffic to confirm that the redirect is actually happening? Also how long has it been since you updated the redirect in Aegir? This might be getting cached so you should try cachebusting the url to make sure (ex. url/?nocacheplease=true)
    – schnippy
    Commented Dec 1, 2013 at 21:57

1 Answer 1

2

Presumably your hook_init runs on both domains.

You need to check you're not already on the redirected page prior to redirection.

e.g

/**
 * Implements hook_init
 */
function mobydetect_init() {
  if($_SERVER['HTTP_HOST'] != 'm.mysite.com'){
    include("PATH/TO/Mobile_Detect.php");
    $detect = new Mobile_Detect();
    if ($detect->isMobile()) {
      drupal_goto('http://m.mysite.com', array('absolute' => TRUE, 'external' => TRUE));
    }
  }
}

Note, hook_init doesn't run on cached pages so you may find mobile users aren't redirected on cached pages. You could try using hook_boot instead or else offload to the webserver conf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.