Skip to content

By Administrator in All, Apache server

The purpose of browser detection is usually to allow the webmaster to provide different content to their web site visitors based on the type and version of the web browser used by a particular visitor. There are several ways to detect the type of a web browser accessing your web site, such as server-side scripting (PHP, ASP, etc…) and client-side scripting(JavaScript). The Apache webserver’s configuration file directives provide an easy and straightforward way to implement browser detection using the User-Agent field of the HTTP header of the browser request packets. We will illustrate how this can be done, using the Apache mod_rewrite module.

As you probably know, using the .htaccess file is usually the most convenient way to set up various Apache configuration directives. For the purpose of this example, you will need to place an .htaccess file in the document root folder of your Apache web server with the following content:

RewriteEngine On

RewriteCond  %{HTTP_USER_AGENT}  Firefox\/7\.0\.1
RewriteRule  ^browser$ /firefox.html [L]

RewriteCond  %{HTTP_USER_AGENT}  ^Lynx
RewriteRule  ^browser$ /lynx.html [L]

RewriteCond  %{HTTP_USER_AGENT}  Chromium
RewriteRule  ^browser$ /chromium.html [L]

RewriteRule  ^browser$ /unrecognized.html [L]

The first line enables mod_rewrite for the current folder. The next lines match the User-Agent header field against different web browser name strings and load different HTML files based on the matched string. For example, let’s say that your domain name is www.example.com. If you type www.example.com/browser into your Firefox 7.0.1 browser, the content of the firefox.html file will be loaded. To avoid any misunderstandings, you should put short descriptive messages into the html files. For example, the firefox.html files could contain something like “This is a Firefox 7.0.1 browser!” :)

The values of the User-Agent field for each browser can be easily obtained by previewing the output of the phpinfo() function. To do this, create a file called i.php with the following content in the document root of your Apache web server:

<?php phpinfo(); ?>

Then, load the www.example.com/i.php URL in each of the desired web browsers and look for the HTTP_USER_AGENT field in the output. Below are the results I obtained by loading the i.php file via Lynx, Chromium and Firefox 7.0.1:

Lynx/2.8.8dev.3 libwww-FM/2.14 SSL-MM/1.4.1

Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko)
Ubuntu/10.10 Chromium/14.0.835.202 Chrome/14.0.835.202 Safari/535.1

Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1

Then, I chose a substring of each field that uniquely identifies the entire field and used it in the condition pattern of the RewriteCond directive.

Of course, this is just a simple example, but it clearly shows how browser detection can be used to serve different web site content to different web browsers.

Please, bear in mind, that browser detection with .htaccess might not be useful in many cases due to a certain poor practice that has been implemented by web browser developers for several years now. More precisely, they have been cramming the UserAgent field with all kinds of strings, which can make it very hard to correctly identify a certain type of browser. As you can see above, my Chromium browser is also pretending to be Mozilla, Safari and AppleWebKit :)

So, should you choose this approach of browser detection, please use it with caution.

Tags: , , , , ,

Comment Feed

No Responses (yet)



Some HTML is OK

or, reply to this post via trackback.