Force SSL and handle browsers without SNI support

有些话、适合烂在心里 提交于 2019-12-06 16:01:34
David Sardari

I decided to drop above htaccess rules due to the various problems. With the following code, web browsers that support Javascript and SNI get redirected to the SSL secured page without a check upon the user agent:

<body>
<?php if (empty($_SERVER["HTTPS"])) { ?>
   <form style="display:none;" method="get" name="redirect"></form>
   <script type="text/javascript">
      var img=document.createElement('img');
      img.src='https://www.example.org/favicon.png';
      img.onload = function() {
         var redirect_form = document.forms["redirect"];
         redirect_form.action = "https:" + window.location.href.substring(window.location.protocol.length);
         redirect_form.submit();
      }
      img.style.display='none';
      document.body.appendChild(img);
   </script>
<?php } ?>
...
</body>

These htaccess rules remove the trailing question mark created by above form:

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]

RewriteCond %{THE_REQUEST} \?\ HTTP [NC] 
RewriteRule .? %{ENV:proto}://%{HTTP_HOST}%{REQUEST_URI}? [R=301,L]

Old web browser that don't support SNI are redirected to http if they access the page over https (What is the most efficient code to detect and redirect SNI supported browsers?):

SetEnv SSL_TLS_SNI %{SSL:SSL_TLS_SNI}

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_USER_AGENT} MSIE\s6 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Windows\sNT\s5 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Android.*(Mobile)?\ [0-3] [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(.*.symbian.*) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(.*.blackberry.*) [NC]
RewriteCond %{SSL:SSL_TLS_SNI} =""
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=307,L]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!