WordPress配置Https(SSL)后页面样式丢失解决方案

当网站升级SSL证书开启https后,很多人都会遇到这些问题:网站页面css样式错乱,图片地址显示错误,网站后台进不去或登录等问题。解决这个问题需要以下几个步骤。

1、修改functions.php文件

打开网站根目录下wp-includes/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、修改wp-config.php文件

打开根目录下wp-config.php文件,在文件最前面添加以下代码:

$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

3、修改网站后台站点地址

打开Wordpress后台,点击设置菜单,将网站地址的http修改为https。

相关