Skip to content

Instantly share code, notes, and snippets.

@oppara
Created June 22, 2018 12:20
Show Gist options
  • Save oppara/3a024a6f8adebc0bef9fb9e35117a73f to your computer and use it in GitHub Desktop.
Save oppara/3a024a6f8adebc0bef9fb9e35117a73f to your computer and use it in GitHub Desktop.
SNI SSL(HTTPS)を使いたいけど Android 2.X とか古いブラウザもあるから SNI 対応ブラウザだけ SSL 接続させる正規表現

SNI SSL(HTTPS)を使いたいけど Android 2.X とか古いブラウザもあるから SNI 対応ブラウザだけ SSL 接続させる正規表現

↑タイトル長えwwwww

Cloudflare で SNI SSL をやりたくなって、需要あるかなと思ってのメモ。

正規表現

(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|Android.*(Mobile)?\ [0-2]\.)

ガラケーにも対応させる場合

(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|UP\.Browser/|DoCoMo/\d\.|SoftBank/\d\.|Android.*(Mobile)?\ [0-2]\.)

ガラケーって基本的に SNI の SSL をサポートされていないからな。

bot や クローラー を HTTPS でアクセスさせない場合

(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|UP\.Browser/|DoCoMo/\d\.|SoftBank/\d\.|Android.*(Mobile)?\ [0-2]\.|bot|Bot|Y!J\-|Crawler|Baidu|Yeti|Slurp)

基本的な bot・クローラーに対してだけの対処。

SNI 非対応ブラウザを考えて、検索結果は普通に HTTP だけど SNI 対応ブラウザだけ HTTPS させたい場合に。

逆に bot 類のを取れば検索結果は HTTPS だし、若干検索順位で優位だけど、SNI 非対応ブラウザで1回警告が出る(スクショ)からな。。。

Google や Yahoo! JAPAN(Google と同じだが一応)、Yahoo! USA、bing(bingbot って名前だから bot で弾ける)、そのほかメジャーなキーワードで弾ければいいかな程度で。

書き方

Cloudflare Flexible じゃない(VPSなど、サーバー側で SSL が使える)場合。

.htaccess の書き方(例)###

# HTTP(S) Stuff
# Redirect all users except doesn't support modern browser, nor Japan cell-phone to HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_USER_AGENT} !(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|UP\.Browser/|DoCoMo/\d\.|SoftBank/\d\.|Android.*(Mobile)?\ [0-2]\.)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]

web.config での書き方(例)

  • 既存の web.config があればそれにくっつける形で。
  • XML ではなく、IIS マネージャーからやる場合はこの XML を参考に適宜。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<system.webServer>
		<rewrite>
			<rules>
				<rule name="HTTP → HTTPS (if SNI OK)" stopProcessing="false">
					<match url="^(.*)$" />
					<conditions>
						<add input="{HTTPS}" pattern="off" />
						<add input="{HTTP_USER_AGENT}" pattern="(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|Android.*(Mobile)?\ [0-2]\.)" negate="true" />
					</conditions>
					<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
				</rule>
				<rule name="HTTPS → HTTP (if SNI NG)" stopProcessing="false">
					<match url="^(.*)$" />
					<conditions>
						<add input="{HTTPS}" pattern="on" />
						<add input="{HTTP_USER_AGENT}" pattern="(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|Android.*(Mobile)?\ [0-2]\.)" negate="false" />
					</conditions>
					<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" />
				</rule>
			</rules>
		</rewrite>
	</system.webServer>
</configuration>

Cloudflare で Flexible SSL をする場合

レンタルサーバー+レン鯖SSLでお金を掛けたくない人向けだね。↑のやつだと使えない!

.htaccess の場合

# HTTP(S) Stuff
# Redirect all users except doesn't support modern browser, nor Japan cell-phone to HTTPS
RewriteCond %{HTTP_CF_VISITOR} !^\{"scheme":"https"\}$
RewriteCond %{CF_VISITOR} !^\{"scheme":"https"\}$
RewriteCond %{HTTP_USER_AGENT} !(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|UP\.Browser/|DoCoMo/\d\.|SoftBank/\d\.|Android.*(Mobile)?\ [0-2]\.)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]

web.config の場合

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<system.webServer>
		<rewrite>
			<rules>
				<rule name="Cloudflare SNI 1" stopProcessing="false">
					<match url="^(.*)$" />
					<conditions>
						<add input="{HTTP_CF_VISITOR}" pattern="^\{"scheme":"https"\}$" negate="true" />
						<add input="{CF_VISITOR}" pattern="^\{"scheme":"https"\}$" negate="true" />
						<add input="{HTTP_USER_AGENT}" pattern="(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|Android.*(Mobile)?\ [0-2]\.)" negate="true" />
					</conditions>
					<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
				</rule>
				<rule name="Cloudflare SNI 2" stopProcessing="false">
					<match url="^(.*)$" />
					<conditions>
						<add input="{HTTP_CF_VISITOR}" pattern="^\{"scheme":"https"\}$" negate="false" />
						<add input="{CF_VISITOR}" pattern="^\{"scheme":"https"\}$" negate="false" />
						<add input="{HTTP_USER_AGENT}" pattern="(MSIE [1-6]\.|MSIE [78]\.\d\; Windows NT 5\.|Android.*(Mobile)?\ [0-2]\.)" negate="false" />
					</conditions>
					<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" />
				</rule>
			</rules>
		</rewrite>
	</system.webServer>
</configuration>

備考

  • CF_VISITORHTTP_CF_VISITOR 結局どっちかは未検証のためわからない。。
    • 分かったら誰かコメントお願いします←
  • 冗長性を考えると{"scheme":"https"} は \{(.*)"scheme":((.*){0}| )"https"((.*){0}|;(.*))\} になるのk・・・いやなんでもないw - 考えすぎかも。

余談:このメモについて

  • 先日Cloudflareが SNI の SSL を無料で提供されるという話を聞いて
  • 『え、マジ!?やったー!じゃあ早速サイトを全部 SSL 化・・・したいところだが』
  • 『古い IE は切り捨てられるとしても、Android 2.X(SNI 非対応)使っている人も少なくないんだよなー・・・』
  • 『うーん、せめて SNI 対応ブラウザだけでも SSL アクセスさせられないか』

という経緯があってこのメモができたのだw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment