We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13166
    • 24 Posts
    Great Script, works perfect. But can how I define exceptions like for the iPad?
      • 4041
      • 788 Posts
      First find out what the ipad uses to identify itself, then add that name to the $uamatches array near the bottom of the script.
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 13166
        • 24 Posts
        Sorry, but at the moment the iPad will be recognized as an mobile device through the "mobi" string. But I want to see the normal website, not the mobile site. I worry about removing the "mobi" string from the uamatches. It would be great to add a new variable like $exclude and add an new if cause. But I have no PHP knowledge.

        Thanks
        István
          • 4041
          • 788 Posts
          Give this version a try, add the names you want to exclude to the $exclude array.

          <?php
          /*     Mobile_redirect
                 01/28/11    6:14 am est
                 11/24/09   10:31 am est
          
                 original code: http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers
                 usage: call in the top of your template
                 [!Mobile_redirect? &mID=`1`!]
                 [!Mobile_redirect? &url=`http://redirectdomain.com`!]
          
                 link back to main page:
                 add &views=1 to the link
                 ex: <a href="http://yourmainsite.com?views=1">Link Back to Main site</a>
          */
          
          // set the MODx document ID of the mobile page
          $mid = isset($mID) ? $mID : "1";
          
          // set the url you want to redirect to
          // http://domain.com   or   http://sub.domain.com
          $url = isset($url) ? $url : '';
          
          // add any names you want to exclude
          $exclude = array("mobi");
          
          if(isset($_GET['views']) || $_SESSION['views'] =='1'){
              $_SESSION['views'] = 1;
          
          }else{
                  if(isset($_SERVER["HTTP_X_WAP_PROFILE"]) || preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) {
                      if($mid =="" && $url !=""){
                          header("Location: ".$url."");
                      }else{
                          $goto = $modx->makeUrl($mid);
                          $modx->sendRedirect($goto);
                      }
                  }
          
                  if(isset($_SERVER["HTTP_USER_AGENT"])){
                      if(preg_match("/Creative\ AutoUpdate/i",$_SERVER["HTTP_USER_AGENT"])) { return false; }
                      $uamatches = array("midp", "j2me", "avantg", "iphone", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");
                      foreach($uamatches as $uastring){
                          if(in_array($uastring, $exclude)){
                              return;
                          }
                          if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) //return true;
                              if($mid =="" && $url !=""){
                                  header("Location: ".$url."");
                              }else{
                                  $goto = $modx->makeUrl($mid);
                                  $modx->sendRedirect($goto);
                              }
                      }
                  }
          }
          ?>
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 13166
            • 24 Posts
            It’s the right way, but it doesn’t work. Perhaps it’s my fault, because my english is bad.

            I changed the content of $exclude to iPad and add "mobi" to the $uamatches, because I want any User Agent with Mobile in the detection.

            The iPad User Agent string is: Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; de-de) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5

            The string contains "mobi" and "iPad". The script has to compare the two variables and only if the result is false the script redirect to mobile website.

            I thinkt that’s the way.

            Tahnks
            István
              • 13166
              • 24 Posts
              A friend helped me and result is the following script:
              <?php
              /*     Mobile_redirect
                     01/28/11    1:40 pm cet
                     01/28/11    6:14 am est
                     11/24/09   10:31 am est
              
                     original code: http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers
                     usage: call in the top of your template
                     [!Mobile_redirect? &mID=`1`!]
                     [!Mobile_redirect? &url=`http://redirectdomain.com`!]
                     [!Mobile_redirect? &exclude=`iPad`!]
              
                     link back to main page:
                     add &views=1 to the link
                     ex: <a href="http://yourmainsite.com?views=1">Link Back to Main site</a>
              */
              
              // set the MODx document ID of the mobile page
              $mid = isset($mID) ? $mID : "1";
              
              // set the url you want to redirect to
              // http://domain.com   or   http://sub.domain.com
              $url = isset($url) ? $url : '';
              
              // add any names you want to exclude
              $exclude = isset($exclude) ? explode(',', $exclude) : array();
              
              if(isset($_GET['views']) || $_SESSION['views'] =='1'){
                  $_SESSION['views'] = 1;
              }else{
                  if(isset($_SERVER["HTTP_X_WAP_PROFILE"]) || preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) {
                      if($mid =="" && $url !=""){
                          header("Location: ".$url."");
                      }else{
                          $goto = $modx->makeUrl($mid);
                          $modx->sendRedirect($goto);
                      }
                  }
                  if(isset($_SERVER["HTTP_USER_AGENT"])){
                      $uastring = $_SERVER["HTTP_USER_AGENT"]; 
                      if(preg_match("/Creative\ AutoUpdate/i",$_SERVER["HTTP_USER_AGENT"])) { 
                          return false;
                      }
                      $check_for_mobiles = true;
                      foreach($exclude as $not_mobil){
                          if( stripos($uastring, $not_mobil) !== false ) {
                              $check_for_mobiles = false;
                              break;
                          }
                      }
                      
                      if ( $check_for_mobiles == true ) {
                          $mobiles = array("midp", "j2me", "avantg", "iphone", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "mobi", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");
                          foreach($mobiles as $mobil){
                              if( stripos($uastring, $mobil) !== false ){
                                  if($mid =="" && $url !=""){
                                      header("Location: ".$url."");
                                  }else{
                                      $goto = $modx->makeUrl($mid);
                                      $modx->sendRedirect($goto);
                                  }
                                  exit;
                              }
                          }
                      }
                  }
              }
              ?>

                • 19931
                • 1 Posts
                Just a quick SEO note - I would set up your redirect to go to http://domain.com/m/ instead of using a submdomian http://m.domain/com - we are getting much better indexing with a directory /m/ instead of a subdomain.

                Hope this helps!

                Gus
                http://www.MobileWebsites.com
                http://facebook.com/mobilewebsites
                http://twitter.com/mobilewebsites2
                  • 9710
                  • 24 Posts
                  I’m also working on a mobile version for several websites.

                  My question: Is there a (maybe easy) way in modx to create such a thing without loosing a lot of performance by deactivating the cache? Can I write a plugin which tries to get the right version from cache? I’ve got no idea how to solve this.

                  Just creating a new context and copying every resource really sucks..

                  // edit:
                  Is here a way to use 2 cache systems so I can define which to use in the plugin?
                    • 27330
                    • 884 Posts
                    is that a typo near the or
                    // set the url you want to redirect to
                    // <a href="http://domain.com" target="_blank" rel="nofollow">http://domain.com</a>
                       or   <a href="http://sub.domain.com" target="_blank" rel="nofollow">http://sub.domain.com</a>
                     
                    $url = isset($url) ? $url : '';
                     


                    I can't get this to work on evo. I get page not found and there is no redirection
                      • 27330
                      • 884 Posts
                      I ended up using mobile detect script from http://detectmobilebrowsers.com/ which works sweet.