Redirect Website Requests Based on the Browser Used (Chrome, Firefox or IE)

As promised in our previous article (How to Perform Internal Redirection with mod_rewrite), in this post we will explain how to display a custom website content using Apache mod_rewrite redirect requests based on the user’s browser criteria.

In theory, all modern browsers should interpret content equally. However, some implement the latest features faster than others. In order to have a fully-functional website that does not break when it is viewed using a certain browser. Unfortunately, this will require a redirection to a different directory or page.

Suggested Read: 5 Tips to Boost the Performance of Your Apache Web Server

The following rewrite rules will redirect requests for tecmint.html to tecmint-chrome.html, tecmint-firefox.html, or tecmint-ie.html depending on the browser being used (Google Chrome, Mozilla Firefox, or Internet Explorer).

To do so, the HTTP_USER_AGENT environment variable is used to identify the browser based on the user-agent string. Here we introduce the RewriteCond directive, which allows us to specify a condition that must be met in order for the redirection to take place.

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

Please note that the target page tecmint.html does not necessarily have to exist. First off, let’s create tecmint-firefox.html, tecmint-chrome.html, and tecmint-ie.html with the following contents.

tecmint-firefox.html:
<!DOCTYPE html>
<html>
  <head>
	<meta charset="utf-8">
	<title></title>
  </head>
  <body>
	<h3>Welcome to Tecmint on Firefox!</h3>
  </body>
</html>
tecmint-chrome.html:
<!DOCTYPE html>
<html>
  <head>
	<meta charset="utf-8">
	<title></title>
  </head>
  <body>
	<h3>Welcome to Tecmint on Chrome!</h3>
  </body>
</html>
tecmint-ie.html:
<!DOCTYPE html>
<html>
  <head>
	<meta charset="utf-8">
	<title></title>
  </head>
  <body>
	<h3>Welcome to Tecmint on Internet Explorer!</h3>
  </body>
</html>

we will see the result of browsing to tecmint.html using different browsers:

Check Site Based on Browser
Check Site Based on Browser

As you can see, requests for tecmint.html were redirected accordingly depending on the browser used.

In this article we have discussed how to do redirect requests based on the user’s browser. To wrap up, I’d highly recommend you take a look at the mod_rewrite cheat sheet and bookmark the redirect and remapping guide in the Apache docs for future reference.

As always, feel free to use the comment form below if you have any questions or feedback about this article. We look forward to hearing from you!

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Gabriel Cánepa
Gabriel Cánepa
Contributor
About the author

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

8 Comments

Leave a Reply
  1. Thank you,

    For all of the popular browser, you can use the following code. I hope it will help someone.

    RewriteEngine on
    
    RewriteCond %{HTTP_USER_AGENT} Opera
    RewriteRule ^index.php$ opera.php [NC,L]
    
    RewriteCond "%{HTTP_USER_AGENT}"  ".*Trident.*"
    RewriteRule ^index.php$ ie.php [NC,L]
    
    RewriteCond %{HTTP_USER_AGENT} Edge
    RewriteRule ^index.php$ edge.php [NC,L]
    
    RewriteCond %{HTTP_USER_AGENT} Chrome
    RewriteRule ^index.php$ chrome.php [NC,L]
    
    RewriteCond %{HTTP_USER_AGENT} Safari
    RewriteRule ^index.php$ safari.php [NC,L]
    
    RewriteCond %{HTTP_USER_AGENT} Firefox
    RewriteRule ^index.php$ firefox.php [NC,L]
    
    RewriteRule ^index.php$ default.php [L]
    
    Reply
  2. Hi thanks for this tutorial however for me it doesn’t work. I must be doing something wrong but don’t know what. I followed your instructions and created http://198.91.92.112/tecmint.html but I get the rewritecond stuff appearing as text on the page. Is it supposed to go within tags? if so, what tags? thanks for any advice.

    Reply
      • thanks for responding. I’ve already got the redirect stuff in the section of the html code but it doesn’t work and just appears as text on the page. I’ve also created a .htaccess file with the redirect lines in it but it still doesn’t work. Thanks for any further advice.

        Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.