How to Redirect Based on Web Browser in Apache

redirect based on web browser in apache

Generally, all web browsers render content similarly, with minor differences. However, some are faster than the others and support advanced features such as CSS animation and HTML5. So you can improve user experience by redirecting users based on their web browsers so that your website doesn’t break down on legacy browsers, and also takes advantages of latest features in modern browsers. Let us take a look at how to redirect based on web browser in Apache.


How to Redirect Based on Web Browser in Apache

Here are the steps to redirect based on web browser in Apache.

Before you proceed, please ensure that you have enabled .htaccess (mod_rewrite) in Apache. If not, then here’s how you can do it:


Place your .htaccess file in the root document folder of your website (/var/www/html)


1. Open .htaccess file

Open a terminal session or SSH into your Linux system. Open .htaccess file in a text editor

$ sudo vim /var/www/html/.htaccess


2. Redirect Based on Web Browser

Let’s say you have a web page but you want to redirect chrome users to, firefox users to and IE users to

To do so, we will use the environment variable HTTP_USER_AGENT in Apache which stores the browser name. We will use RewriteCond to redirect based on specific conditions.

Add the following lines to redirect based on browser name

RewriteCond "%{HTTP_USER_AGENT}" ".*Firefox.*"
RewriteRule "^/webpage\.html$" "/webpage-firefox.html" [R,L]
RewriteCond "%{HTTP_USER_AGENT}" ".*Chrome.*"
RewriteRule "^/webpage\.html$" "/webpage-chrome.html" [R,L]
RewriteCond "%{HTTP_USER_AGENT}" ".*Trident.*"
RewriteRule "^/webpage\.html$" "/webpage-ie.html" [R,L]


In the above code, Apache web server will check the HTTP_USER AGENT value for each request, and redirect users o appropriate web page, based on their browser name.


3. Restart Apache web server

Restart Apache web server to apply changes

$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]


That’s it! Apache will now redirect based on user’s web browser.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!