What is the most efficient code to detect and redirect SNI supported browsers?

user3243499 picture user3243499 · Jun 1, 2014 · Viewed 8.4k times · Source

Say, I have a website mywebsite.com, hosted using Apache Httpd. Now what I want is that whenever any user types mywebsite.com or www.mywebsite.com and if the browser supports SNI then it should redirect to https://www.mywebsite.com else redirect to http://www.mywebsite.com.

So, what is the most efficient way to achieve it ?

Answer

Utkal Sinha picture Utkal Sinha · Jun 1, 2014

The below code should work

Options -Indexes +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^mywebsite.com$
RewriteCond %{HTTPS} (on|off)
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=302,L]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_USER_AGENT} !MSIE\s6
RewriteCond %{HTTP_USER_AGENT} !Windows\sNT\s5
RewriteCond %{HTTP_USER_AGENT} !^(.*.symbian.*) [NC]
RewriteCond %{HTTP_USER_AGENT} !^(.*.blackberry.*) [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=302,L]

Here we are neglecting most of the browsers which does not support SNI and hence for them only the http version would be loaded.