9

I want to redirect visitors to my specific page based on which browser they are using.

For example: all visitors will first visit http://www.example.com/xy/abc.html.

If the visitor is using firefox, he should be redirected to http://www.example.com/xy/firefox.html

If the visitor is using chrome, he should be redirected to http://www.example.com/xy/chrome.html

I want to handle this for opera, chrome, firefox, IE, safari browser and with a default redirect for non identified browser.

Any help with the code? I tried looking up and all I found was a single redirect code for all browser rather than a specific one.

Your help is appreciated. Thanks.

vinny
  • 529
  • 4
  • 7
  • 16

2 Answers2

16

You can use mod_rewrite's RewriteCond directive to test against the reported name of the user agent. In a .htaccess file placed in your xy directory, it'd look like this:

RewriteCond %{HTTP_USER_AGENT} Opera
RewriteRule ^abc.html$ http://example.com/xy/opera.html [R=301]

This will permanently redirect browsers that have Opera somewhere in their user agent string to opera.html. You can find a decent list of how user agents identify themselves on useragentstring.com. You'll notice that many user agent strings actually start with "Mozilla" (due to some wicked historical reasons), but simply testing to see if the string contains the browser's name (IE is "MSIE") should be enough.

Problem is, the HTTP_USER_AGENT string is reported by the browser, and the browser may pretty much report anything they want. Opera even has a built-in option to make it masquerade itself as IE or FF. Generally, there's a good chance that browser-sniffing based on the user agent string will eventually miss, and then the user will be annoyed. I'd strongly suggest you to leave the user some way to override the automatic redirection.


So, something like this might work as a first approach:

RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} Opera
RewriteRule ^abcd.html$ opera.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} MSIE
RewriteRule ^abcd.html$ ie.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Chrome
RewriteRule ^abcd.html$ chrome.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Safari
RewriteRule ^abcd.html$ safari.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Firefox
RewriteRule ^abcd.html$ firefox.html [NC,L]

RewriteRule ^abcd.html$ default.html [L]

The L flag makes sure that that rule is the Last to be executed in that pass, so if a browser reports itself with a string containing Firefox, Safari, MSIE and Opera, then the first rule to match (in this case Opera) will determine the landing page. This version performs a soft redirect (the browser's address bar won't change). If you want a hard, R=301 redirect, make sure to spell out the entire URL of the landing page, i.e. RewriteRule ^abcd.html$ http://example.com/xy/opera.html [NC,L,R=301]

SáT
  • 3,633
  • 2
  • 33
  • 51
  • Thanks. I am actually not very concerned about if it fails for some users. Not really expecting a 100% working solution but anything close to 85% should be fine. – vinny Dec 27 '12 at 16:35
  • Just to clarify, I put together this code, does this look fine to you?
    RewriteCond %{HTTP_USER_AGENT} Opera RewriteRule ^abc.html$ opera.html [R=301] RewriteCond %{HTTP_USER_AGENT} Mozilla RewriteRule ^abc.html$ ff.html [R=301]
    Another issue, chrome also used Mozilla, which means the redirect would not be accurate for chrome users.
    – vinny Dec 27 '12 at 16:41
  • Added a sample implementation. Seems to work on my machine, but make sure to test it :). – SáT Dec 27 '12 at 17:02
  • Thanks that worked perfect. Really appreciate your help. I was wondering what user agent, I should use for Android, iPhone, Windows Phone devices? Do you have any idea? – vinny Dec 29 '12 at 17:13
  • It still depends on the browser (useragentstrings.com has [a list for mobile browsers](http://www.useragentstring.com/pages/Mobile%20Browserlist/)), though browsers do tend to report their platform in the UA, so you could check against just "Android" etc. However, if you just want to see if the user's using a mobile device, it's better to check for the Profile or x-wap-profile http header fields. See [this question](http://stackoverflow.com/questions/3680463/mobile-redirect-using-htaccess) for details. – SáT Dec 29 '12 at 22:07
  • Could you please look on inverse problem http://stackoverflow.com/questions/39924802/htaccess-allow-only-from-chrome-opera-mozilla-safari-redirect-from-others – Tarlan Mammadzada Oct 07 '16 at 19:50
0

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]

REF : https://www.tecmint.com/mod_rewrite-redirect-requests-based-on-browser/