跳到主要内容

网站开启https之后图片默认仍是http的解决方法

一般情况下,对网站有要求的站长或者公司,一般都会启用https,Secured http是更安全的网站形式,但是,如果站内有一个小图片没有使用http,都不会出现小锁头。

Web层配置SSL


然而,在web层如果已经通过Apache等已经配置好了https的SSL,查看如何增加https安全锁的文章在本站搜索:如何给网站加个安全锁https。

应用层配置SSL

那么第二步,可以检查一下Application应用层。

不管你用的是哪种CMS,应该有个默认网站URL的位置,所以,这里配置一下,基本上就可以解决这个问题,你不防试一下。

数据层配置SSL-历史图片

当然这个只可以解决以后的新上传的图片,那么历史图片怎么处理呢?
要么通过函数的方式,在Application应用层进行修改,要么通过SQL直接在数据库里面修改。
Application应用层:
在function.php里面增加如下代码 (适用新旧域名不一致/一致的两种情况)

  1. /* 替换图片链接为 https */
  2. function https_image_replacer($content){
  3. if( is_ssl() ){
  4. /*已经验证使用 $_SERVER[‘SERVER_NAME’]也可以获取到数据,但是貌似$_SERVER[‘HTTP_HOST’]更好一点*/
  5. $host_name = $_SERVER[‘HTTP_HOST’];
  6. $http_host_name=’http://’.$host_name.’/wp-content/uploads’;
  7. $https_host_name=’https://’.$host_name.’/wp-content/uploads’;
  8. $content = str_replace($http_host_name$https_host_name$content);
  9. }
  10. return $content;
  11. }
  12. add_filter(‘the_content’, ‘https_image_replacer’);

简化(前提是域名没有修改的前提下):

  1. function replacehttp($content){
  2. if( is_ssl() ){
  3. $content = str_replace(‘http://域名/wp-content/uploads’, ‘https://域名/wp-content/uploads’, $content);
  4. }
  5. return $content;
  6. }
  7. add_filter(‘the_content’, ‘replacehttp’);

Database数据层:

  1. PDATE wp_posts SET post_content = replace(post_content, ‘http://域名/wp-content/uploads’,’https://域名/wp-content/uploads’);

EBRP-Lite-单域名商业SSL证书280元/年|Plus泛域名

返回顶部