Help us understand the problem. What is going on with this article?

mod_rewriteでHTTP / HTTPSの切り替え

More than 3 years have passed since last update.

問い合わせフォームと管理画面だけ強制的に https でアクセスさせたい場合のメモ。
いろいろと調べて試行錯誤した結果、.htaccessに以下のように書いて実現した。

/.htaccess
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # HTTPでアクセスされた場合、
  RewriteCond %{HTTPS} off
  # 以下のURLは常に https にリダイレクトする
  RewriteCond %{HTTP_HOST} www.example.com
  RewriteCond %{REQUEST_URI} ^/admin/.*$ [OR]
  RewriteCond %{REQUEST_URI} ^/inquiry/.*$ 
  # ただし以下は除く
  RewriteCond %{REQUEST_URI} !^/assets/.*$
  RewriteCond %{REQUEST_URI} !^.*\.(js|css|gif|jpg|png|ico)$
  # https にリダイレクト
  RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

  # HTTPSでアクセスされた場合、
  RewriteCond %{HTTPS} on
  # 以下のURL以外は常に http にリダイレクトする
  RewriteCond %{HTTP_HOST} www.example.com
  RewriteCond %{REQUEST_URI} !^/admin/.*$
  RewriteCond %{REQUEST_URI} !^/inquiry/.*$
  # ただし以下は除く
  RewriteCond %{REQUEST_URI} !^/assets/.*$
  RewriteCond %{REQUEST_URI} !^.*\.(js|css|gif|jpg|png|ico)$
  # http にリダイレクト
  RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
  • %{HTTP_HOST}でホストを限定してるのは、開発環境で無効化するため
  • cssや画像ファイルをリダイレクトしてしまうと https ON のページで暗号化されてないコンテンツを読み込んでしまうので除外
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした