如何配置WordPress使其通过HTTP而非HTTPS与指定URL通信?
解决方案:强制WordPress保留特定HTTP URL不转HTTPS
方法1:内容输出时替换目标URL
在主题的functions.php中添加以下代码,拦截文章/页面内容里被转成HTTPS的目标地址,替换回原始HTTP版本:
add_filter('the_content', 'preserve_specific_http_url'); function preserve_specific_http_url($content) { $target_http = 'http://your-target-domain.com/path'; $content = str_replace(str_replace('http://', 'https://', $target_http), $target_http, $content); return $content; }
这个过滤器会在内容渲染前执行,只修正你指定的URL,不影响其他HTTPS链接的安全性。
方法2:拦截HTTPS跳转请求
如果站点全局强制HTTPS跳转,通过wp_redirect钩子排除目标URL的跳转:
add_filter('wp_redirect', 'exclude_url_from_ssl_redirect', 10, 2); function exclude_url_from_ssl_redirect($location, $status) { $target_http = 'http://your-target-domain.com/path'; $target_https = str_replace('http://', 'https://', $target_http); if ($location === $target_https) { return $target_http; } return $location; }
当WordPress尝试将目标URL跳转到HTTPS时,这个钩子会强制返回原始HTTP地址。
方法3:允许HTTP协议在内容中存在
如果WordPress的内容过滤(KSES)移除了HTTP协议,添加规则允许它:
add_filter('wp_kses_allowed_protocols', 'allow_http_for_specific_url'); function allow_http_for_specific_url($protocols) { if (!in_array('http', $protocols)) { $protocols[] = 'http'; } return $protocols; }
注意:这个方法会全局允许HTTP协议,建议配合方法1使用,避免全站暴露在非安全链接风险中。
额外提示
- 修改主题文件前务必备份,防止语法错误导致站点无法访问。
- 若使用SSL类插件(如Really Simple SSL),直接在插件设置的“例外URL”中添加目标HTTP地址,无需修改代码。
- 仅对必要的URL做HTTP例外,保持站点其他部分的HTTPS加密状态。
内容的提问来源于stack exchange,提问作者구병희
相关产品推荐
相关产品推荐

