track ip addresses, phone numbers, etc

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

Published on 10 April 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 to, this is what to do.

Using PHP

if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {

Using Perl

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

if (index($ENV{'HTTP_USER_AGENT'}, 'iPad')) {
    print "Location:\n\n";

Using JavaScript

Use the navigator object in JavaScript.

if (navigator.userAgent.match(/iPad/i)) {

Using Nginx webserver

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

server {
    listen      80;
    location / {
        if ($http_user_agent ~ iPad) {
            rewrite     ^(.*)$1 permanent;

Using Apache webserver

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

RewriteCond %{HTTP_USER_AGENT} ^.iPad.$
RewriteRule ^(.*)$ [R=301]

Using Lighttpd webserver

Edit lighttpd.conf and add this:

$HTTP["useragent"] =~ "iPad" {
    url.redirect = ( "^/(.*)" => "$1" )

Created on 10 April 2010

Affiliate Disclosure: Some of the links to products on this blog are affiliate links. It simply means, at no additional cost to you, we’ll earn a commission if you click through and buy any product.