Knowledgebase

How to set your website to display different content depending on the client's browser

With the help of mod_rewrite you can redirect content depending the internet browser of the client accessing your site. For example, you may want to redirect requests for a file to a similar file, or any file, if the client uses a particular browser.

This can be done with a few Apache directives placed in an .htaccess file on your hosting account. As an example let's assume that you want to redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php if the client uses a particular web browser. We'll review how you can do this for the most popular browser. The directives are practically the same; you only need to change the relative argument of the RewriteCond directive according to the browser for which you want the redirect to apply.

Using the above mentioned example, if you want to redirect requests coming from Google Chrome, you can use the following directives placed in the .htaccess file that's in the public_html folder of your hosting account:

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php
#when the request is made from the browser Google Chrome

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Chrome [NC]
RewriteRule ^index\.php$ /index2.php

So with the above directives all requests that come from Google Chrome for yourdomain.com/index.php will be redirected to yourdomain.com/index2.php. Requests coming from other browsers will not be redirected.

In case you want the same example redirect to apply to requests made from Microsoft's Internet Explorer, you can use the following directives:

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php
#when the request is made from Microsoft's Internet Explorer

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} MSIE [NC]
RewriteRule ^index\.php$ /index2.php

As you can see from the above two examples the only difference is the part in the RewriteCond directive that comes after the variable HTTP_USER_AGENT; for Google Chrome it's just Chrome and for Internet Explorer it's MSIE. This part is included in the user agent HTTP header; that's how the browser is identified when a request is made.

If you want the redirect to apply to requests made with Mozilla Firefox, you should use directives such as these:

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php
#when the request is made from Mozilla Firefox

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Firefox [NC]
RewriteRule ^index\.php$ /index2.php

The redirect will also work with SeaMonkey. This is an internet suite whose browser is similar to Firefox; its user agent field in the HTTP header is almost the same. In case you want the redirect to apply only to requests made from Firefox and not to those from SeaMonkey, you can use directives such as these:

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php
#when the request is made from Mozilla Firefox but don't redirect requests from SeaMonkey

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Firefox [NC]
RewriteCond %{HTTP_USER_AGENT} !SeaMonkey [NC]
RewriteRule ^index\.php$ /index2.php

On the other hand, if you want the redirect to apply only to requests from SeaMonkey and not to those from Firefox, just remove from the above directives the exclamation mark before SeaMonkey.

To redirect requests from the web browser Opera use the following directives:

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php
#when the request is made with the browser Opera

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Opera [NC]
RewriteRule ^index\.php$ /index2.php

You can use various combinations in case you want to redirect requests to a few different files depending on the browser. For example, if you place the following directives in the .htaccess file that's in the public_html directory on you hosting account:

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php
#if the requests are made with Google Chrome or Internet Explorer

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Chrome [NC,OR]
RewriteCond %{HTTP_USER_AGENT} MSIE [NC]
RewriteRule ^index\.php$ /index2.php

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index3.php
#if the requests are made with Mozilla Firefox

RewriteCond %{HTTP_USER_AGENT} Firefox [NC]
RewriteRule ^index\.php$ /index3.php

requests for yourdomain.com/index.php that are made with Google Chrome and Internet Explorer will be redirected to yourdomain.com/index2.php. Those made with Mozilla Firefox will be redirected to yourdomain.com/index3.php. Requests from other browsers will not be redirected.

You can also apply redirects only when a specific versions of a particular browser is used. For example, the following redirects will work only for Firefox version 15 and for Internet Explorer 8:

#Redirect requests for yourdomain.com/index.php to yourdomain.com/index2.php
#only for requests coming from Firefox version 15 and Internet Explorer 8

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "MSIE 8\.*" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Firefox/15\.* [NC]
RewriteRule ^index\.php$ /index2.php

With the above rules if the requests are made, for example, from Firefox 14 or Internet Explorer 9, the redirects will not work.

Needless to say, if you use any of the examples in this article you have to change the paths in the RewriteRule directive according to the location of the content that you want to redirect and the location of the content that's going to replace it.

For general information about the mod_rewrite directives and their syntax check out the tutorial on mod_rewrite.

Was this answer helpful?

 Print this Article

Also Read