If not viewed on mobile then go to main site

I used a script that worked great, basically if your coming to our site away from a pc you will be re-directed to the mobile site, works perfectly.

<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) )     
{
var url = "http://www.mysite.com/mobile/<?php echo $actual_link ?>";  
$(location).attr('href',url);
}

But I wanted to add to it so that if they try to access the mobile site whilst on a pc they get re-directed back to the main site, so tried this and it doesnt work.

It goes into a re-direct loop and the page never loads.

<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) )     
{
var url = "http://www.mysite.com/mobile/<?php echo $actual_link ?>";  
$(location).attr('href',url);
} else {
var url = "http://www.mysite.com/<?php echo $actual_link ?>";
$(location).attr('href',url);
}

Ah not htinking straight, I need to put that not mobile script on the mobile site, not on the main site.

So basically will a if not work, as it doesnt seem to be

<script>
if(! /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) )     
{
var url = "http://www.mysite.com/<?php echo $actual_link ?>";  
$(location).attr('href',url);
}
</script>

Ok its strange as it works, the bit that doesnt work is the inside bit

var url = "http://www.checksafetyfirst.com";  
$(location).attr('href',url);

Tested with an alert and its reading the if statement but not actioning the redirect

ok all sorted, just used

window.location.replace("http://www.mysite.com");

instead

That doesn’t work at all as soon as one of your visitors changes THEIR useragent to contain what THEY want it to contain. The useragent is barely reliable enough to use for statistics - it should never be used for anything else.

You should be using responsive web pages that rearrange themselves based on the available viewport width - the type of device is irrelevant…

Hi felgall,

Yes I agree with you, that really there should only be the 1 site, but I do have my reasons this time, and have explained to the management here that what I should now is develop the mobile site so that it then works on a pc.

The reason I didnt was time, as they needed it for the sales team to show off at a conference, and with the site being what it is, its not an easy site to make it fully responsive, so at this time it was decided that it would be better to have 2 sites.

You have concerned me though, and put me right as you always do, as didnt know about the user agent issue, so will have to look into it. The script that Im using does a job but I know its not ideal.

You might find mobiledetect is a more complete way to check.

But phone screens vary in size, as do tablet screens, as well as laptop and desktop monitors. So in reality, both the “mobile” and “desktop” sites would need to be responsive to work on them all.
I don’t see how making two responsive sites, or one responsive and another fixed, is quicker or easier than making just one responsive site.

2 Likes

The functionality of the main site was too complicated to make responsive at this time, and the search options to get to hotels are what out visitors are used to, and we also noticed that similar sites to us use 2 different sites. I also had a working template, and with the time it was easier to only use the minimum of info and functionality than try and fit it all in, but what I’m going to do now is develop the mobile site to then be the 1 site, but will have to wait for time to be given to me to do it.

Here are the sites, if you fancy a look
CSF Main
CSF Mobile

1 Like

Cheers gandalf458,

That looks a lot more robust than my simple script, so will use it for sure.

Including mobile_detect.php as shown, in the code below is it just the case of using redirect inside the if statement.

// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) { 
   header('Location: http://mobile.example.com/');
}

As above?

So if correct, for my purposes I only need to use the 2 scripts as below -

.require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect; 
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) { 
    header('Location: http://mobile.example.com/');
} 
// Any tablet device
.if( $detect->isTablet() ){ 
    header('Location: http://mobile.example.com/');
}

To take that further, what would be the code to use to put on the mobile site to send people to the main site if they trying to view the mobile version on the pc for instance

I missed that your post was in the JS forum. Mobile_detect is PHP so I don’t think a mixed JS/PHP redirect like your “above” script will work.

It’s a while since I used this script, having made my sites responsive, but I thought there was a way to check mobile phone and tablet with one test.

As @SamA74 says, either way it is a bit of a hack, so you might need to try it and see an couple of times…

Why not just pick a maximum width that you intend to treat as mobile and simply redirect to the mobile version for anything under that viewport width? If you r main site doesn’t fit on mobile devices then it will not fit narrow browsers either.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.