一、开启https访问域名301重定向
借助.htaccess 文件实现 301 重定向,编辑网站根目录的.htaccess,加入以下代码:
RewriteCond %{HTTPS} !on [NC] RewriteRule (.*) http://xxx.cn%{REQUEST_URI} [R=301,NC,L]
或者
#网站定制化开启 HTTPS 的 301 重定向 RewriteCond %{SERVER_PORT} !^443$ RewriteCond %{HTTP_USER_AGENT} !MSIE/[1-8]\. [NC] RewriteCond %{HTTP_HOST} www.tuhongwei.com RewriteRule ^.*$ https:/域名%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # BEGIN WordPress # 在`BEGIN WordPress`与`END WordPress`之间的指令(行)是 # 动态生成的,只应被WordPress过滤器修改。 # 任何对标记之间的指令的修改都会被覆盖。 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
二、登录和后台强制开启 SSL
修改 WP-config.php 文件,直接在文件末尾加入以下代码:
/* 强制后台和登录使用 SSL */
$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
三、让站内链接支持 SSL
上传到空间的附件都被 WordPress 标记为了绝对链接,一般需要修改数据库,但这种方法比较危险,因此推荐另一种方法
代码法,编辑当前主题下的 function.php 文件,加入以下代码:
/* 替换图片链接为 https */ function my_content_manipulator($content){ if( is_ssl() ){ $content = str_replace('http://xxx.cn/wp-content/uploads', 'http://xxx.cn/wp-content/uploads', $content); } return $content; } add_filter('the_content', 'my_content_manipulator');
版权声明