.htaccess Redirect to specific webpage based on browser

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]