LoginSignup
10
5

More than 3 years have passed since last update.

WordPressで作成されたサイト全体をHTTPS化してみた。

Last updated at Posted at 2018-04-27

各種環境(いろんなサーバ)に合わせて「ググれば簡単にできるはずー」と思っていたら、わりとHTTPS化って結構苦労するのねと思いましたので、こちらに控えておきます。

AWSの場合

ALB or ELB or NLBを利用している場合

nginx編

EC2(nginx) + ELB or ALB or NLBで構築された環境の場合は以下のように設定すれば上手く行きます。

nginx.conf
server {
    listen       *:80;
    server_name  example.co.jp;
    root   /var/www/;
    index  index.php index.html;

    if ($http_x_forwarded_proto != https) {
      return 301 https://$host$request_uri;
    }
}
wp-config.php
$_SERVER['HTTPS']='on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php'); // ここよりも上に書いておけば良い。

参考記事:
https://qiita.com/snoguchi/items/f5ccb67592f87942480d

httpd編

EC2(httpd) + ELB or ALB or NLBで構築された環境の場合は以下のように設定すれば上手く行きます。

\.htacces.txt
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Port} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

txtなり何かしらの拡張子にしないと出てこないのね。。

wp-config.php
$_SERVER['HTTPS']='on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php'); // ここよりも上に書いておけば良い。

参考記事:
https://webcreator.me/blog/236/

EC2インスタンスのみの場合

nginx編

EC2(nginx) + ELB or ALB or NLBで構築された環境の場合は以下のように設定すれば上手く行きます。

nginx.conf
server {
    listen       *:80;
    server_name  example.co.jp;
    root   /var/www/;
    index  index.php index.html;

    if ($HTTPS != off) {
      return 301 https://$host$request_uri;
    }
}
wp-config.php
$_SERVER['HTTPS']='on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php'); // ここよりも上に書いておけば良い。

適当書いたけど、nginxの場合はリバースプロキシ使うのが正解っぽい気がしました。
あとで修正します。

httpd編

EC2(httpd)のみで構築された環境の場合は以下のように設定すれば上手く行きます。

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

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
wp-config.php
$_SERVER['HTTPS']='on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php'); // ここよりも上に書いておけば良い。

参考記事:
https://qiita.com/foursue/items/58e74c4a8c946dd49e12

さくらインターネットの場合

これは記事を漁れば出てくるのですが、以下のような設定を行えば、SSL化できます。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/wp-cron.php$
RewriteCond %{HTTP:X-Sakura-Forwarded-For} ^$
RewriteRule ^(.*)$ https://test.sakura.ne.jp/$1 [R,L]
</IfModule>
wp-config.php
if( isset($_SERVER['HTTP_X_SAKURA_FORWARDED_FOR']) ) {
    $_SERVER['HTTPS'] = 'on';
    $_ENV['HTTPS'] = 'on';
    $_SERVER['HTTP_HOST'] = 'example.co.jp';
    $_SERVER['SERVER_NAME'] = 'example.co.jp';
    $_ENV['HTTP_HOST'] = 'example.co.jp';
    $_ENV['SERVER_NAME'] = 'example.co.jp';
}

define('DB_NAME', 'year_wordpress'); // ここより上に書いておけば良い。

参考記事:
https://qiita.com/tabimoba/items/64ef60412abe7ad6f0ac

まとめ

plugin使えばいいとか、.htaccessだけでできるとかwp-config.phpだけ書けばいいとか古いサーバやオンプレだったら別にいいんですけど、今時のクラウドサーバ時代に合わない記事が多いこと多いこと…。
今度、wp-config.phpの定義についてまとめます。
(wp-config.phpを調べてたらこれ設定しておかないと危険かも?と思う設定がいくつか見つかりました。)

10
5
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
10
5