Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess Redirect to specific webpage based on browser

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.

like image 336
vinny Avatar asked Dec 27 '12 16:12

vinny


1 Answers

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]

like image 106
SáT Avatar answered Oct 22 '22 18:10

SáT