WordPress 全站 HTTPS 迁移实战:证书、301 跳转与收录保留
WordPress 全站 HTTPS 迁移的核心顺序是:先让 https:// 完全可用,再改站点地址,最后做 301 与收录迁移。本文按可执行命令展开,并修正证书有效期等过时信息。
问题背景与根因:为什么不能先改站点地址再上证书
很多旧教程把“改站点地址”放在第一步,这是最容易锁死后台的顺序。WordPress 的 home 与 siteurl 一旦写成 https://example.com,后台登录、主题资源、AJAX 请求都会立即走 TLS;此时若证书链不完整、SAN 不含 www、80 端口未放行或 DNS 未生效,浏览器会报 NET::ERR_CERT_DATE_INVALID、ERR_CERT_COMMON_NAME_INVALID,管理员被 301 到无法访问的 https 地址。正确做法是先在服务器层部署证书,用 curl 与 openssl 确认 https://example.com 和 https://www.example.com 均返回 200,再迁移 WordPress 地址。
收录丢失的根因通常不是 HTTPS 本身,而是 http 与 https 被搜索引擎视为两个 URL:旧 http URL 没有 301、canonical 仍指向 http、sitemap 未更新、Search Console 未添加 https 资源。另一个过时事实是证书有效期:公开信任的 TLS 证书最长有效期为 398 天,Let’s Encrypt 免费证书为 90 天,必须配置自动续期,不能按旧文章里的“一年一换”操作。迁移前可用 wp option get home 和 wp option get siteurl 记录旧值,便于回滚。
迁移前准备:备份、DNS、CSR 与证书有效期检查
先做可回滚备份:wp db export backup.sql 与 tar czf wp-content-backup.tar.gz wp-content。检查 DNS 解析:dig +short example.com、dig +short www.example.com,确保 A/AAAA 指向当前服务器。若使用商业证书,生成 CSR 的命令为 openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr -subj '/CN=example.com',提交 CA 时 SAN 必须同时包含 example.com 与 www.example.com。Let’s Encrypt 可直接执行 sudo certbot --nginx -d example.com -d www.example.com,证书落在 /etc/letsencrypt/live/example.com/。
证书有效期要按新规检查:商业证书最长 398 天,Let’s Encrypt 90 天。用 sudo certbot renew --dry-run 验证续期,用 systemctl list-timers | grep certbot 确认定时器存在。迁移前不要同时改 DNS、证书和站点地址;可先用 curl -I --resolve example.com:443:203.0.113.10 https://example.com 绕过 DNS 测试源站证书。若证书链缺中间证书,浏览器会报 curl: (60) SSL certificate problem: unable to get local issuer certificate,Nginx 必须使用 fullchain.pem 而不是 cert.pem。
第一步:在服务器层启用 HTTPS 并用 curl/openssl 验证
先只加 443 监听,不改 WordPress 地址。Nginx 示例:
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
root /var/www/wordpress;
index index.php;
}
执行 nginx -t && systemctl reload nginx。验证命令:curl -I https://example.com 应返回 HTTP/2 200;openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates -subject -issuer 应显示 notAfter 在 90 天或 398 天有效期内,且 subject/SAN 含两个域名。若报 SSL_ERROR_BAD_CERT_DOMAIN,说明 SAN 不匹配;若报 nginx: [emerg] cannot load certificate '/etc/letsencrypt/live/example.com/fullchain.pem': BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory),检查路径与权限。此阶段 http:// 仍可访问是正常的,下一步才做 301。
第二步:修改 WordPress 站点地址与 WP-CLI 数据库替换
确认 https 可访问后,再改地址。先执行 wp option update home 'https://example.com' 与 wp option update siteurl 'https://example.com'。然后做数据库替换,先 dry-run:wp search-replace 'http://example.com' 'https://example.com' --all-tables --precise --skip-columns=guid --dry-run --report-changed-only。参数含义:–all-tables 覆盖自定义表;–precise 安全处理 PHP 序列化数据;–skip-columns=guid 保留文章永久标识,避免 RSS 与聚合重复;–report-changed-only 只显示有变化的表。确认输出后去掉 –dry-run 执行,成功会显示 Success: Made N replacements。
若后台因地址错误无法登录,可在 wp-config.php 临时加入 define('WP_HOME','https://example.com'); define('WP_SITEURL','https://example.com');,修复后再移除。替换后执行 wp cache flush 与 wp rewrite flush。若命令报 Error: This does not seem to be a WordPress installation.,在命令后加 --path=/var/www/wordpress。检查残留:wp db query 'SELECT COUNT(*) FROM wp_posts WHERE post_content LIKE ...',把 LIKE 写成 '%http://example.com%',结果应为 0;主题选项与自定义字段中的旧链接也要用同一命令覆盖。
第三步:服务器层 301 强制跳转配置示例
跳转必须在服务器层做 301,不要依赖插件。Nginx 在 80 端口 server 中写:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
Apache 可用:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
执行 nginx -t && systemctl reload nginx 或 apachectl configtest && systemctl reload apache2。验证 curl -I http://example.com 应返回 HTTP/1.1 301 Moved Permanently 与 Location: https://example.com/;curl -I http://www.example.com 应跳到统一域名。HSTS 不要一上来就设一年:先加 add_header Strict-Transport-Security 'max-age=300; includeSubDomains' always;,确认全站无 http 资源后再改为 31536000。使用 Cloudflare 时 SSL/TLS 模式选 Full (strict),避免 Flexible 造成重定向循环。
混合内容与证书链排查:真实报错与修复命令
F12 控制台出现 Mixed Content: The page at 'https://example.com/' was loaded over HTTPS, but requested an insecure stylesheet 'http://example.com/wp-content/themes/theme/style.css'. This request has been blocked; the content must be served over HTTPS. 说明仍有硬编码 http。先在数据库层替换,再查文件:grep -R 'http://example.com' wp-content/themes/ wp-content/plugins/ --include='*.php' --include='*.css' --include='*.js'。主题选项、小工具、自定义字段常漏掉,用 wp search-replace 'http://example.com' 'https://example.com' --all-tables --precise --skip-columns=guid 覆盖。CSP 的 Content-Security-Policy: upgrade-insecure-requests 只能兜底,不能替代替换。
证书类报错要分清:NET::ERR_CERT_DATE_INVALID 查系统时间与续期;ERR_CERT_COMMON_NAME_INVALID 查 SAN 是否含 www;ERR_SSL_PROTOCOL_ERROR 查 443 监听与 TLS 版本;curl: (60) SSL certificate problem: certificate has expired 立即续期并 reload。Nginx 报 BIO_new_file() failed 多为 fullchain.pem 路径错或权限不足,用 ls -l /etc/letsencrypt/live/example.com/ 确认。修复后清浏览器缓存与 CDN 缓存再测。
收录迁移:Search Console、sitemap 与 canonical 验证
在 Google Search Console 同时保留 http 资源并新增 https 资源,推荐添加“网域”资源并完成 DNS 验证,再添加 URL 前缀 https://example.com 便于看具体页面。提交 sitemap:WordPress 5.5+ 核心为 https://example.com/wp-sitemap.xml,Yoast 为 https://example.com/sitemap_index.xml。robots.txt 中写 Sitemap: https://example.com/wp-sitemap.xml,不要用 robots 屏蔽 http 旧站,否则 301 信号无法传递。执行 wp rewrite flush 后访问 sitemap 确认 URL 全为 https。
检查 canonical:curl -s https://example.com/sample-post/ | grep -i 'rel=.canonical',应输出 https 绝对地址。用 curl -I http://example.com/sample-post/ 确认 301 到 https 同一路径。Search Console 的“地址更改”工具可在旧 http 资源中指向新 https 资源,Bing 网站管理员工具同样提交。迁移后 2-4 周观察覆盖率、404 与重定向报告;不要同时改固定链接结构,否则 301 链路会叠加。
方案对比与效果验证:插件跳转、Nginx、Apache 怎么选
跳转方式对性能与稳定性的影响如下:
| 方式 | 性能 | 维护 | 风险 |
|---|---|---|---|
| WordPress 插件跳转 | 每次请求进 PHP | 简单 | 插件停用即失效 |
| Nginx 301 | 内核层返回 | 需改配置 | 规则写错会循环 |
| Apache 301 | 模块层返回 | 需改虚拟主机 | 未加载模块时要用 Redirect |
证书选择也要按有效期管理:
| 证书 | 有效期 | 续期 | 适用 |
|---|---|---|---|
| Let’s Encrypt | 90 天 | certbot 自动 | 绝大多数站点 |
| 商业 DV/OV | 最长 398 天 | 手动或 API | 需要品牌与合规 |
| 自签名 | 自定义 | 手动 | 仅测试,不被浏览器信任 |
效果验证清单:curl -I http://example.com 看 301;curl -I https://example.com 看 200;curl -sI https://example.com | grep -i 'strict-transport-security' 看 HSTS;openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates 看 notAfter;curl -s https://example.com | grep -o 'http://[^[:space:]]*' | head 查混合内容。全部通过后再在 Search Console 观察索引。
常见问题
先检查 home 与 siteurl 是否都是 https 且与证书域名一致;可在 wp-config.php 临时定义 WP_HOME 与 WP_SITEURL,清除浏览器 Cookie 后重试;再用 curl -I https://example.com/wp-admin/ 看是否 301 循环,修正 Nginx 规则后移除临时常量。
会,但 certbot 定时器会自动续期。用 sudo certbot renew –dry-run 验证,续期后执行 systemctl reload nginx;商业证书最长 398 天,也要设置到期监控,不能按旧文的一年期管理。
不要。guid 是文章永久标识,改成 https 可能影响 RSS 与聚合识别。使用 wp search-replace 时加 –skip-columns=guid,只替换正文、选项、自定义字段等需要更新的列。
可以临时保留,但生产环境建议服务器层 301。插件跳转依赖 PHP 与插件启用状态,停用即失效;配置 Nginx/Apache 301 后停用插件,并用 curl -I http://example.com 验证 Location 正确。
先清浏览器与 CDN 缓存,再检查主题选项、小工具、自定义字段和 CSS/JS 硬编码;用 wp search-replace 全表替换并 grep 主题插件目录。CSP 的 upgrade-insecure-requests 只能兜底,不能替代修复。