http でアクセスされた時に https に mod_rewrite でリダイレクトしたメモ。

最近 https じゃないと駄目と上から言われること多いので
http のリクエストが来た時に apache で https にリダイレクトするようにしたメモ。


環境

  • OS: CentOS 6.6
  • Apache: 2.2.15

mod_rewrite モジュールを読み込む

コメントアウトしてある場合は読み込むようにします。

/etc/httpd/conf/httpd.conf
1
LoadModule rewrite_module modules/mod_rewrite.so

対象のバーチャルホストにリダイレクトの設定を追記する

mod_rewrite の処理を追記します。

/etc/httpd/conf.d/example.com.conf
1
2
3
4
5
6
7
8
9
10
<VirtualHost *:80>
ServerName hoge.example.com

<ifModule mod_rewrite.c>
RewriteEngine On
LogLevel alert rewrite:trace3
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</ifModule>
</VirtualHost>

apache 再起動して確認してみる

apache を再起動後に、 http でアクセスしてみて https にリダイレクトされれば成功です。

以上です。

© 2024 磁力式駆動 All Rights Reserved.
Theme by hiero