0

I tried all of the suggestions on this page (Using .htaccess to redirect obsolete browsers) but none work. I've googled but did not find a working example.

None of these work when dealing with browsers with two digit version numbers.

For example, the line below blocks Opera v.1.x and Opera v.12.x:

RewriteCond %{HTTP_USER_AGENT} "Opera/1\." [NC,OR]

The codes below do not work either:

RewriteCond %{HTTP_USER_AGENT} "Opera/[1-11]\." [nc,or]
RewriteCond %{HTTP_USER_AGENT} "Opera/[1-9][0-1]?\." [nc,or]

What's the correct code to redirect Opera versions 1.x-11.x?

I did some more testing and found something that is odd to me. If I try to access the site with Opera version 11, the rewrite rule reluctantly kicks in when using the following:

RewriteCond %{HTTP_USER_AGENT} "Opera/9\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/8\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/7\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/6\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/5\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/4\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/3\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/2\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/1\." [NC,OR]

If I simply comment out...

RewriteCond %{HTTP_USER_AGENT} "Opera/9\." [NC,OR]

...the Opera browser version 11 does not get redirected.

To be more specific, I'm trying to redirect certain browsers with certain versions to a particular page if they match the rules. Here is a sample but it does not work as intended and I think there's got to be a more concise way to write each condition:

<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "Firefox/3\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Firefox/2\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Firefox/1\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/9\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/8\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/7\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/6\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/5\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/4\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/3\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/2\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/1\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 7\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 6\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 5\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 4\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 3\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 2\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE 1\." [NC]
RewriteRule . http://www2.mysite.com/page.php [L]
</ifmodule>

Here's what I'm trying to do:

<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "Firefox/if less than version 3\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/if less than version 10.1\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE if less than version 7\." [NC]
RewriteRule . http://www2.mysite.com/page.php [L]
</ifmodule>
Community
  • 1
  • 1
Jeff
  • 495
  • 1
  • 5
  • 18

2 Answers2

3

The code below does what I want:

<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "Firefox/[1-3]\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Opera/9\..*Version/(1[10]|[1-9])\. [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Opera/[1-8]\." [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "MSIE [1-7]\." [NC]
RewriteRule . http://www2.mysite.com/page.php [L]
</ifmodule>

Thank you Jon Lin! Your code suggestions helped me to understand a little better :)

Jeff
  • 495
  • 1
  • 5
  • 18
  • It will work only if you are not redirect visitors of "http://www2.mysite.com". If you want to use this redirection within same website, you'll need to exclude this page to avoid redirection loop. Use "RewriteCond %{REQUEST_URI} !^www2.mysite.com/page.php [NC]" – Yevgeniy Afanasyev Nov 10 '14 at 05:36
1

Try:

RewriteCond %{HTTP_USER_AGENT} Opera/(1[10]|[1-9])\. [NC,OR]

Seeing as how Opera's user-agent doesn't follow the format that you'd expect, just need this line to exclude opera 12:

RewriteCond %{HTTP_USER_AGENT} !Opera/9\..*Version/12\. [NC,OR]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Can you explain how the version number part (1[10]|[1-9]) works? I need to apply this condition to different versions of different browsers as well. – Jeff Apr 16 '13 at 18:59
  • @Jeff The `()` section gives 2 options separated by the `|` character. The first is `1[10]` and the other is `[1-9]`. The first matches **10** or **11**, and the second matches **1** through **9**. So both combined, it matches **1** through **11**. – Jon Lin Apr 16 '13 at 19:03
  • @Jeff it doesn't work because [Opera version 12 gives a User-Agent string of `Opera/9.80`](http://myip.ms/view/comp_browsers/376/Opera_12.html). You need to do a bit more research on what it is that you're trying to match – Jon Lin Apr 16 '13 at 19:20
  • @Jeff As you can see in the link that provides Opera V12 user agents, it has a `Version/12.` string in it, so why not just match against that? as in `RewriteCond %{HTTP_USER_AGENT} Opera [OR] RewriteCond %{HTTP_USER_AGENT} !Version/12\.`? – Jon Lin Apr 16 '13 at 19:21
  • Sorry. I didn't realize Opera's UA was set up that way. Everything is making more sense now. I edited my original post to show what I'm trying to accomplish. How do I write the conditions so any version less than what I specify (e.g., Opera 10.1) is redirected? Sorry I wasn't more clear to begin with. Thank you for your help. – Jeff Apr 16 '13 at 19:58