SSL - yagisawatakuya/Wiki GitHub Wiki

SSL対応

SSL対応手順

https://nelog.jp/wordpress-ssl

一部が安全でない原因

・wp_head()とwp_footer() ・画像の絶対パスがhttpの場合 ・formタグのaction ・wp関数、アイキャッチ画像

WordPress で自動的に出力される URL などの修正

functions.php

function wbsSSLURL($args) {
	$args = preg_replace("/s?https?:\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?/i", '', $args);
	return $args;
}
function wbsSSLURL_plugin($args) {
	$args = preg_replace("/s?https?:\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/wordpress/i", '', $args);
	return $args;
}
// add_filter('home_url', 'wbsSSLURL');
add_filter('site_url', 'wbsSSLURL');
add_filter('template_directory_uri', 'wbsSSLURL');
add_filter('stylesheet_uri', 'wbsSSLURL');
add_filter('plugins_url', 'wbsSSLURL_plugin');
 
function wbs_url($ssl = false)
{
	$url = get_home_url(null, '', null);
	if($ssl === true) {
		$url = str_replace('http://', "https://", $url);
	} else {
		$url = str_replace('https://', "http://", $url);
	}
	echo $url;
}

http://weble.org/2011/03/17/wordpress-ssl

WordPress で URL をすべて相対 URL にする方法

class relative_URI {
    function relative_URI() {
        add_action('get_header', array(&$this, 'get_header'), 1);
        add_action('wp_footer', array(&$this, 'wp_footer'), 99999);
    }
    function replace_relative_URI($content) {
        $home_url = trailingslashit(get_home_url('/'));
        return str_replace($home_url, '/', $content);
    }
    function get_header(){
        ob_start(array(&$this, 'replace_relative_URI'));
    }
    function wp_footer(){
        ob_end_flush();
    }
}
new relative_URI();

https://dogmap.jp/2011/03/18/wordpress-relative-url/

wp_head wp_footer 出力内容をhttpに置換する

/*
 * wp_head wp_footer 出力内容をhttpに置換する
*/
// wp_head wp_footer  出力フィルタリング・ハンドラ追加
add_action( 'wp_head', 'wp_head_buffer_start', 0 );
add_action( 'wp_head', 'wp_head_buffer_end', 100 );
add_action( 'wp_footer', 'wp_footer_buffer_start', 0 );
add_action( 'wp_footer', 'wp_footer_buffer_end', 100 );
 
// wp_head 出力フィルタリング
function wp_head_filter($buffer) {
  $buffer = str_replace('http://', 'https://', $buffer);
  return $buffer;
}
 
// wp_head バッファリング開始
function wp_head_buffer_start() {
  ob_start( 'wp_head_filter' );
}

// wp_head バッファリング終了
function wp_head_buffer_end() {
  ob_end_flush();
}

// wp_footer 出力フィルタリング
function wp_footer_filter($buffer) {
  $buffer = str_replace('http://', 'https://', $buffer);
  return $buffer;
}

// wp_footer バッファリング開始
function wp_footer_buffer_start() {
  ob_start( 'wp_head_filter' );
}

// wp_footer バッファリング終了
function wp_footer_buffer_end() {
  ob_end_flush();
}

http://company.miyanavi.net/archives/1302

wp_get_attachment_url をSSL対応

function fix_ssl_attachment_url($url) {
    if(is_ssl()){
        $url = preg_replace("/^http:/", "https:", $url);
    }
    return $url;
}
add_filter("wp_get_attachment_url", "fix_ssl_attachment_url");

http://takacworks.com/archives/57

https化によるリダイレクトループを解消する

wp-config.php

$_SERVER['HTTPS'] = 'on';

https://wp-doctor.jp/blog/2016/07/18/wordpress%E3%83%AF%E3%83%BC%E3%83%89%E3%83%97%E3%83%AC%E3%82%B9%E3%81%AE%E3%83%AA%E3%83%80%E3%82%A4%E3%83%AC%E3%82%AF%E3%83%88-%E3%83%AB%E3%83%BC%E3%83%97%E3%81%AE%E5%8E%9F%E5%9B%A0%E3%81%A8/

管理画面での SSL 通信

wp-config.php

// SSLによる ログインと管理画面へのアクセスを強制する
define('FORCE_SSL_ADMIN', true);

https://wpdocs.osdn.jp/%E7%AE%A1%E7%90%86%E7%94%BB%E9%9D%A2%E3%81%A7%E3%81%AE_SSL_%E9%80%9A%E4%BF%A1

HTTPSへのサイト移行時に合わせcanonicalの更新が重要

rel=”canonical”が指すURLがHTTPのままですと、こちらが処理がされてしまう為、 rel=”canonical”のURLを必ずHTTPSにおきかえる事が推奨されております。 http://seohopper.jp/staff/canonical201412/