LoginSignup
1
1

More than 5 years have passed since last update.

一部のディレクトリを除いて「https://~」へリダイレクトする方法を試してみた

Posted at

背景

WebサイトをSSL化させる機会があったのですが、SSL化させたいサイトが設置してある数十個のサブディレクトリにそれぞれサイトが設置してあり、今まで何十サイトとSSL化させてきたにも関わらず、初経験だったのですが、一部のディレクトリを除いて「https://~」へリダイレクトさせることにしました。

色々ネット上で調べてみて上手く動作したので、メモです。

.htaccessにするコード

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} www.example.com
RewriteCond %{REQUEST_URI} !^/サブディレクトリ名1/.*$
RewriteCond %{REQUEST_URI} !^/サブディレクトリ名2/.*$
RewriteCond %{REQUEST_URI} !^/サブディレクトリ名3/.*$
RewriteCond %{REQUEST_URI} !^.*\.(js|css|gif|jpg|png|ico)$
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

</IfModule>

まとめ

という感じです。
参考記事によると、マルチディレクトリにも応用できるそうで、機会があれば試してみようかなと思います。

参考サイト

特定ディレクトリのみ共有SSL化する.htaccess設定
https://office-obata.com/report/memorandum/post-682/

一部ディレクトリ以外をSSL(https)へリダイレクトしたい
https://teratail.com/questions/54658

mod_rewriteでHTTP / HTTPSの切り替え
https://qiita.com/gotohiro55/items/7daa988db23a5a8355c1

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1