问题状态:
当HTTPS网页中加载了HTTP资源(如CSS、JS、图片等)时,浏览器会认为页面不安全,显示”Not Secure”警告,并可能导致:
- 🔒 HTTPS显示删除线
- 🚨 “Not Secure”提示
- 💥 页面排版错乱(CSS文件加载失败)
通过修改functions.php 文件修复
在 wordpress后台-》 外观 -> 主题编辑器 → functions.php 末尾添加如下代码:
// 强制HTTPS
function force_https() {
if (!is_ssl() && $_SERVER['HTTP_HOST'] != 'localhost') {
wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);
exit();
}
}
add_action('template_redirect', 'force_https');
// 修复WordPress地址和站点地址
update_option('siteurl', str_replace('http://', 'https://', get_option('siteurl')));
update_option('home', str_replace('http://', 'https://', get_option('home')));
// 修复混合内容
function ssl_src_replace($content) {
if (is_ssl()) {
$content = str_replace('http://', 'https://', $content);
}
return $content;
}
add_filter('the_content', 'ssl_src_replace');
add_filter('widget_text', 'ssl_src_replace');
检查特定位置的混合内容
在浏览器中按 F12 打开开发者工具,查看:
- Console 标签:查看具体的混合内容警告
- Network 标签:查看哪些资源仍然通过HTTP加载
- Security 标签:查看安全报告
wordpress后台域名配置错误错误
如果wordpress后台域名配置错误也会导致WordPress地址使用了IP地址,而网站域名是 www.xxx.com,这将导致了CORS和混合内容问题。
- 登录WordPress后台 (
https://www.xxx.com/wp-admin) - 进入 设置 → 常规
- 确保以下两个地址都使用你的域名:
- WordPress地址(URL):
https://www.xxx.com - 站点地址(URL):
https://www.xxx.com
- WordPress地址(URL):
- 保存更改