track ip addresses, lookup mac addresses, etc

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" )
}
{ % include 'ajrwd' %}

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

← Previous Post Perl one-liners

Play Prince of Persia on Ubuntu Linux using DOSBox Next Post →

{ % include 'footer' %}