3

I created mobile site for existing webpage and in it, I placed the code in mobile folder like www.testing.com/mobile and it works fine.

But what I need to do is if the user visited from mobile it should be redirected to mobile page and user visited from web it goes to default pages.

Here I used:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0" /> 

to fit the page but url shows like m.tesing.com for mobile how to do this

By browser agent or any other ways. Thanks.

user1978142
  • 7,946
  • 3
  • 17
  • 20
papa.ramu
  • 423
  • 1
  • 5
  • 18
  • check [this](http://stackoverflow.com/questions/1118581/redirect-mobile-devices-to-alternate-version-of-my-site) answer. – miltos Apr 25 '14 at 07:31
  • There is a lot of ways to do it, no one way is the best way. – Ryan Apr 25 '14 at 08:38
  • Google is your friend: https://www.google.fr/search?q=php+detect+mobile&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:fr:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=-R5aU4X9JabR8gfq54CwCA – fluminis Apr 25 '14 at 08:39
  • ya solved thanks for giving response – papa.ramu Apr 25 '14 at 09:10

3 Answers3

1

Download this detectmobile.js and make this changes in your head tag

 <head>
                <script src="detectmobile.js"></script>
                <script>
                        try {
                                // This is the URL where mobile
                                detectmobile.defaultMobileURL = "http://m.testing.com";
                                detectmobile.process();
                        } catch(e) {
                                // Make sure that in the fault modes
                                // we do not interrupt execution of other Javascript code
                        }
                </script>
        </head>
Thulasi
  • 116
  • 6
0

Instead of using jquery you can use simple javascript to detect it:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 window.location.replace("http://m.tesing.com");
 //or window.location = "http://m.tesing.com";
}

Or other technique with CSS

/* Smartphones ----------- */
@media only screen and (max-width: 760px) {
  #some-element { display: none; }
}

jQuery:

$( document ).ready(function() {        
    if( $('#some-element').css('display')=='none' {
        window.location.replace("http://m.tesing.com");
     //or window.location = "http://m.tesing.com";    
    }
 });
Adnen Chouibi
  • 400
  • 4
  • 16
0

Use this code for detecting mobile devices and then redirect it

    <script language="javascript">
      if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) 
      {
        window.location = "http://m.domain.com";
      }
    </script>

The test() method has been used to test for a match in a string and if a match is found then a redirection takes place. Add this code at the top of the main index.php page.

Use this code on the mobile index.php to redirect back

    <script language="javascript">
      if( !/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) 
      {
        window.location = "http://testing.com";
      }
    </script>

As for the m.testing.com you will have to create an sub-domain.