Search code examples
iphoneiosweb-applicationshttp-redirectdetection

How can I redirect non-IOS users to another page in my web app


I'm working on a simple web app using jQuery mobile, and it is designed specifically for the iPhone. I am looking for a way to redirect anyone who isn't using an iOS device to another page.

I can't find any recent solutions that work. The closest I got was this, but the script did not work on my iPhone. It redirected me regardless of what device I was using.

Is there a solution that will fit my needs?


Solution

  • Please note the script on that page redirects iPhone and iPod users to another site. It does not redirect non-iPhone/iPod users to another site. If that was the problem, simply change the javascript code to the following:

    if (!(navigator.userAgent.match(/iPhone/i)) && !(navigator.userAgent.match(/iPod/i))) {
        location.replace("http://notiphoneoripod.yourdomain.com");
    }
    

    If you need to redirect iPad as well, use the following modification instead:

    if (!(navigator.userAgent.match(/iPhone/i)) && !(navigator.userAgent.match(/iPod/i)) && !(navigator.userAgent.match(/iPad/i))) {
        location.replace("http://notiphoneoripodoripad.yourdomain.com");
    }
    

    Alternatively, if you are using a server-side programming language, such as PHP, you should do the redirect through that instead of client-side code. You can check using $_SERVER['HTTP_USER_AGENT'] in PHP.

    Or if you want to do it directly on the server, if you're using Apache, you can add the following in .htaccess:

    RewriteCond %{HTTP_USER_AGENT} !^.*iPhone.*$
    RewriteRule ^(.*)$ http://notiphone.yourdomain.com [R=301]