Detect iPad in PHP, Perl, JavaScript, Nginx, Apache, Lighttpd

Published on April 10, 2010

This article shows how to programmatically detect an iPad visitor from the user-agent in PHP, Perl or JavaScript and configure it at webserver level for Nginx, Apache and Lighttpd servers.

The iPad's user-agent string for Safari is (iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

To forward all iPad visitors from http://aruljohn.com to http://ipad.aruljohn.com, this is what to do.

Using PHP


Using Perl

Make sure this is the only print statement. Do not allow any other header to be output before this.

#!/usr/local/bin/perl
if (index($ENV{'HTTP_USER_AGENT'}, 'iPad')) {
    print "Location: http://ipad.aruljohn.com\n\n";
    exit;
}

Using JavaScript

Use the navigator object in JavaScript.

if (navigator.userAgent.match(/iPad/i)) {
    location.href='http://ipad.aruljohn.com';
}

Using Nginx webserver

Edit nginx.conf. Use the variable $http_user_agent to match the user-agent string containing iPad.

server {
    listen      80;
    server_name aruljohn.com;
    location / {
        if ($http_user_agent ~ iPad) {
            rewrite     ^(.*) http://ipad.aruljohn.com$1 permanent;
        }
    }
}

Using Apache webserver

Edit httpd.conf, apache.conf or .htaccess and add this:

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ipad.aruljohn.com [R=301]

Using Lighttpd webserver

Edit lighttpd.conf and add this:

$HTTP["useragent"] =~ "iPad" {
    url.redirect = ( "^/(.*)" => "http://ipad.aruljohn.com/$1" )
}

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on April 10, 2010