Category Archives: PHP

Handling mobile redirects in .htaccess for iPhones/Androids and tablets

Below is code I put into my .htaccess to set an environment variable to change the content/theme for mobile users.


# Mobile
SetEnvIf User-Agent "Mobile" IS_MOBILE
SetEnvIf User-Agent "Googlebot-Mobile" IS_MOBILE
SetEnvIf User-Agent "BlackBerry" IS_MOBILE
SetEnvIf User-Agent "iPod" IS_MOBILE
SetEnvIf User-Agent "iPhone" IS_MOBILE
SetEnvIf User-Agent "NetFront" IS_MOBILE
SetEnvIf User-Agent "Symbian OS" IS_MOBILE
SetEnvIf User-Agent "Windows Phone" IS_MOBILE

Notice that ‘Android’ is not present in the User-Agent check. While testing with a Galaxy Nexus and Nexus 7, I noticed that the easiest way to tell the difference between Android on phones vs. Android on tablets was to check for ‘Mobile’.

From here I can access IS_MOBILE via PHP like so:


if (isset($_SERVER['IS_MOBILE'])) {
include('index-mobile.php');exit;
}