wordpress全站开启HTTPS协议的方法教程

WordPress教程 12633

自从百度站长平台发布的《百度开放收录https站点公告》中表示对相同权值的站点,会优先对待采用https协议的页面后,https瞬间火爆起来,各大小网站纷纷启用https协议。但是很多wordpress用户给服务器安装配置SSL证书开启https协议后发现,网站内容不能正常访问显示了,之所以会这样是因为还没有对wordpress进行对应的修改,下面博客吧分享两种修改方法让wordpress网站全面支持https协议。

方法一:

利用wordpress提供的api,通过修改主题让wordpress支持https。

优点:不涉及数据库,操作简单,不再使用https时只需要把代码删除即可,不会伤及网站。缺点:换主题的时候要重新修改。

代码一:HTTPS绝对链接替换(推荐)

在当前使用主题的functions.php文件中添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
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");
	}
}
代码二:HTTPS相对链接替换

使用相对链接,HTTP和HTTPS双协议共存。

在当前使用主题的functions.php文件中添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
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, '//'), strstr($siteurl, '//'), $content);
			$content = str_replace( 'http:'.strstr($upload_dir['baseurl'], '//'), strstr($upload_dir['baseurl'], '//'), $content);
			return $content;
		}
		ob_start("fanly_ssl_main");
	}
}

以上代码来自:https://zhangzifan.com/wordpress-ssl-link.html

方法二:

修改修改库,把原来的http替换为https。

优点:一劳永逸,绝对https。缺点:需要操作数据库,需要动手能力和理解能力,不再使用https时还要修改数据库。

操作步骤:

1、登陆网站后台,在设置——常规里修改“WordPress地址(URL)”和“站点地址(URL)”为https协议的地址,如下图:

wordpress全站开启HTTPS协议的方法教程

2、登陆网站数据库的phpmyadmin管理页面,在SQL中执行下面的查询语句:

1
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://www.boke8.net','https://www.boke8.net');

作用是把文章内容中原来的附件URL改为https协议的URL,如果不修改的话,附件如图片就不能显示。

如果评论或者文章自定义字段中也使用过网站的地址,那么还要执行以下语句:

1
2
3
4
UPDATE wp_comments SET comment_content = REPLACE(comment_content, 'http://www.boke8.net','https://www.boke8.net');
UPDATE wp_comments SET comment_author_url = REPLACE(comment_author_url, 'http://www.boke8.net','https://www.boke8.net');
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'http://www.boke8.net','https://www.boke8.net');
UPDATE wp_termmeta SET meta_value = REPLACE(meta_value, 'http://www.boke8.net','https://www.boke8.net');

提醒:把代码中博客吧的域名改为自己网站的域名再执行,此外操作前一定要对数据库进行备份

3、进入后台——设置——多媒体中,把文件上传里的“文件的完整URL地址”修改为https协议后的地址,如果原来没有设置完整URL地址,就忽略这一步

wordpress全站开启HTTPS协议的方法教程

附:

百度开放收录https站点公告地址:http://zhanzhang.baidu.com/wiki/392

精品推荐: