阿里云虚拟主机下wordpress实现http转https总结

一、云虚拟主机是不支持https的,不过我们可以通过cdn服务实现,具体可参考官网。

参考链接:https://jingyan.baidu.com/article/6f2f55a17519f1b5b83e6c6b.html

 

二、前台http转https代码统一修改,后台是否可以沿用待研究。
1.在wp-includes\load.php的函数is_ssl()中做如下修改:

function is_ssl() {
        return true; // 添加这一行代码
	if ( isset( $_SERVER['HTTPS'] ) ) {
		if ( 'on' == strtolower( $_SERVER['HTTPS'] ) ) {
			return true;
		}

		if ( '1' == $_SERVER['HTTPS'] ) {
			return true;
		}
	} elseif ( isset($_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
		return true;
	}
	return false;
}

2.在wp-content\themes\twentyseventeen\functions.php后添加代码:

// add by hk
//WordPress SSL
add_filter('get_header', 'fanly_ssl');
function fanly_ssl(){ //绝对地址
    if( is_ssl() ){
        function fanly_ssl_main ($content){
            $siteurl = get_option('siteurl');
            $upload_dir = wp_upload_dir();
            $content = str_replace( 'http:'.strstr($siteurl, '//'), 'https:'.strstr($siteurl, '//'), $content);
            $content = str_replace( 'http:'.strstr($upload_dir['baseurl'], '//'), 'https:'.strstr($upload_dir['baseurl'], '//'), $content);
            return $content;
        }
        ob_start("fanly_ssl_main");
    }
}

三、通过第一、第二步目前,我的网站已经没有任何问题了,如果还出现样式乱问题,可参考下列解决方法,但强烈不推荐。

1.前台样式异常,可在wp-content\themes\twentyseventeen\functions.php后添加代码:

add_filter('script_loader_src', 'agnostic_script_loader_src', 20,2);
function agnostic_script_loader_src($src, $handle) {
return preg_replace('/^(http|https):/', '', $src);
}

add_filter('style_loader_src', 'agnostic_style_loader_src', 20,2);
function agnostic_style_loader_src($src, $handle) {
return preg_replace('/^(http|https):/', '', $src);
}

2.后台样式异常比较麻烦,需要修改的地方比较多。

(1)wp-includes\script-loader.php后添加代码:

add_filter('script_loader_src', 'agnostic1_script_loader_src', 20,2);
function agnostic1_script_loader_src($src, $handle) {
return preg_replace('/^(http|https):/', '', $src);
}

add_filter('wp_style_loader_src', 'agnostic2_style_loader_src', 20,2);
function agnostic2_style_loader_src($src, $handle) {
return preg_replace('/^(http|https):/', '', $src);
}

(2)编辑器问题待修改。

(3)其他问题待发现。

太晚了,好困,今天就记录到这里。加油!明天继续。

 

发表评论