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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With