HTTP_USER_AGENT : Detecting mobile devices / browsers for inocming HTTP requests

By | December 17, 2013

How to redirect incoming HTTP requests from specific devices or browsers ?

You’ll first need a mechanism to detect this data in the incoming HTTP request.

Webserver Administrators may be asked to configure webservers to perform certain actions based on the incoming request data. One such task is to redirect users to mobile version of the website (assuming your setup has one) if the user is accessing the website is using a mobile device such as smart phone or a tablet.
Now a days responsive web designs have made quite an impact; but we can not write off mobile websites yet. Especially businesses still use dedicated mobile websites rather than solely relying on responsive web site design. Many e commerce, CMS and and WCM packages have mobile websites configured at no extra cost/effort.

You might also need to display a specific page to user if he/she is logging in using browser that is not recommended.

HTTP_USER_AGENT

How will the websevrer (Apache or IBM HTTP server for our example) identify the device used by the end user ?
The incoming HTTP request comes with this information. This information can be checked by querying variable HTTP_USER_AGENT. Same variable will also contain information about the browser used by end user.

Full information stored in this variable will look something like this

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

MSIE 6.0” indicates that User is using Microsoft Internet Explorer version 6.0.

OS/System used is a” Windows NT5.1” (which is most probably Windows XP – I’m not an expert here) – but essentially its is a Windows PC.

This variable can be queried inside Apache/IBM HTTP server using RewriteCond conditions (%{HTTP_USER_AGENT}) or used in Other server side scripts such as PHP ($_SERVER[‘HTTP_USER_AGENT’]).

Example of redirecting a user to mobile site based on the user-agent is give below

RewriteEngine on
 
	RewriteCond %{HTTP_COOKIE}  !viewfullsite [NC]
	RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera mobile|palmos|webos" [NC]
	RewriteRule   ^/web/$  /web/mobile/ [L,R=303]

More details can be found in Apache documentation here – https://httpd.apache.org/docs/trunk/rewrite/remapping.html#browser-dependent-content