0

I have a responsive site built, and the homepage is redundant on mobile, I would like to be able to redirect to a specific internal page. I have found solutions to redirect to a mobile version of a site, but this does not work as I get a redirect loop or you cant visit any other pages on the site.

For instance I want to redirect: http://www.url.com/home

to

http://www.url.com/portfolio

On mobile only. And only that one page.

matt
  • 985
  • 4
  • 12
  • 28
  • Have a look here for the best way to detect mobiles http://stackoverflow.com/a/3540295/1301076 the redirection part should be trivial, just window.location(...) – rjdown Oct 24 '14 at 18:30

3 Answers3

1

Insert this code at http://www.url.com/home file

var isMobile = {
Android: function() {
    return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
    return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
    return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
    return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
},
any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}};
if( isMobile.any() ) {
    window.location.assign("http://www.url.com/portfolio");
}
elti musa
  • 590
  • 2
  • 6
  • 14
1

This can easily be achieved by using some JavaScript. The code in the if statement detects if the user is using a mobile device (Android, iOS, Kindle, BlackBerry, Windows Phone, Kindle, etc.) and the code inside the if statement simply redirects the user to the link you specify.

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
    window.location.replace("http://www.url.com/portfolio");
}
Baraa
  • 1,271
  • 11
  • 8
0

If you are using php, there are libraries that can do mobile detect. Google actually provides one: https://code.google.com/p/php-mobile-detect/

On mobile detect set location to http://www.url.com/portfolio before anything is sent to client.

If it's javascript it's a little tricky but you can get a general idea of the user agent and set a variable to what kind of device.

What is the best way to detect a mobile device in jQuery?

Once you have that variable you can do a document.location to the redirect URL.

Community
  • 1
  • 1
Liquidchrome
  • 836
  • 4
  • 9
  • The linked SO thread also has several comments about the downsides of UA sniffing. A somewhat more solid approach (and probably more performant as it doesn't use a huge RegEx) is here: http://stackoverflow.com/a/4819886/258598 (make sure to review comments, too). – cautionbug Oct 24 '14 at 18:16