将站点从 http 迁移到 https 和 SSL

问题描述:

过去我很幸运,当我必须将站点从 HTTP 迁移到 HTTPS 并安装 SSL 证书时,我的主机会这样做并且一切正常.

I've been lucky enough in the past when I have to migrate a site from HTTP to HTTPS and install a SSL Cert that I my host does it and everything just works.

我现在正在与一位主持人合作,我认为我需要更多地亲自动手.

I'm working with a host now where I think I will need to be more hands on.

我在 Mysql 和 Apache 堆栈上使用 Wordpress.迁移这个的正确方法是什么.通过数据库进行搜索和替换,然后通过 .htaccess 文件重定向?

I'm using Wordpress on a Mysql and Apache stack. What would be the proper way to migrate this. Doing a search and replace via the database and then redirects via the .htaccess file?

目前,我使用https://example.com".但是,处理 http 和 https 到https://example 的从 www 到非 www 的重定向的正确方法是什么.com".有一些 Wordpress 插件可以强制"使用 SSL,但我听说这些插件可以做意想不到的事情.

Currently, I have "https://example.com" working. However, what is the proper way to handle the redirects from www to non-www for both the http and https to "https://example.com". There are Wordpress plugins that "force" SSL but I have heard these can do unexpected things.

谢谢

首先,我将解释不使用 .htaccess 文件的过程:假设你有你的 wordpress 网站说http://example.com"

First, I'll explain the procedure without the use of a .htaccess file: Assuming that you have your wordpress site in place say "http://example.com"

  1. 安装 ssl 证书(按照发行人)
  2. 转到您的数据库;wp_options 表和更改'siteurl' 和 'home' 选项值以 https 开头而不是http 和 offcourse 保存更改.
  3. 登录您的 WordPress 管理面板,然后转到设置 » 常规.单击保存选项.这将确保在其他任何需要更正的地方更正网站网址.
  4. 然后转到设置 » 固定链接并点击保存以确保所有帖子链接都正常工作.

  1. Install the ssl certificate (follow the instructions give to you by the issuer)
  2. Go to your database; wp_options table and change 'siteurl' and 'home' option value to begin with https instead of http and offcourse save the changes.
  3. Login to your WordPress admin panel, and go to Settings » General. Click save Options. This will ensure that the site url is corrected anywhere else that needs to be.
  4. Then go to Settings » Permalink and click Save to ensure that all post links are working fine.

安装这个插件搜索&替换并激活.这个插件应该通过更新路径来帮助修复图像和断开的链接,而无需编写代码;这很容易解释.

Install this plugin Search & Replace and activate. This plugin is supposed to help in fixing images and broken links by updating paths without you having to write code; it's pretty self explanatory.

是的,我同意您的观点,即 WP Force SSL 插件为网站带来了奇迹,我已经尝试过了,但我并没有从中吸取教训,但我仍然会尝试一下.此时,您的https://example.com" 可以完美运行

And yes I agree with you that WP Force SSL plugin does wonders to a website, I've tried it and I learn't a lesson the hard way, but still I'll try it out. At this point you'll have your "https://example.com" working perfectly

其次,我将解释在不使用任何插件的情况下执行相同的过程.我的假设和上面一样.

Secondly, I'll explain doing the same procedure without using any plugin. My assumption is same as above.

  1. 重复上面的第 1 步和第 2 步.
  2. 不要离开你的数据库,我们需要使用 SQL Query 在 MySQL 中进行搜索和替换.我在下面的 SQL 查询中只列出了一些需要替换的内容

  1. Repeat step 1 and 2 above.
  2. Don't leave your database yet, we need to do a search and replace in MySQL using SQL Query. I've listed just a few of things needed to be replaced in the SQL query below

UPDATE wp_posts 
SET guid = replace(guid, 'http://example.com', 'https://example.com');

UPDATE wp_posts 
SET post_content = replace(post_content, 'http://example.com', 'https://example.com');

UPDATE wp_postmeta 
SET meta_value = replace(meta_value,'http://example.com', 'https://example.com');

注意:根据您拥有的表格,对每个表格进行搜索和替换总是好的

NB: Depending on the tables you have, its always good to do a search and replace per table

要从 http 重定向到 https,我们将使用 .htaccess 文件.在 .htaccess 文件

To redirect from http to https we'll use .htaccess file. Try the following with mod_rewrite in your .htaccess file

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

http://www.askapache.com/htaccess/http-https-rewriterule-redirect.html

如果您的提供者禁用了 .htaccess,您也可以在 PHP 中解决这个问题(这不太可能,因为您要求它,但无论如何)

You can also solve this from within PHP in case your provider has disabled .htaccess (which is unlikely since you asked for it, but anyway)

if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    if(!headers_sent()) {
        header("Status: 301 Moved Permanently");
            header(sprintf(
            'Location: https://%s%s',
            $_SERVER['HTTP_HOST'],
            $_SERVER['REQUEST_URI']
        ));
        exit();
    }
}

到目前为止,这两种方法 https 都应该运行良好.

Until this point with both methods https should be working well.

然后,要将非 www 重定向到 www,请直接在 .htaccess 文件中的 RewriteEngine On 下方使用以下代码:

Then, to redirect non-www to www, use the following code directly below RewriteEngine On in the .htaccess file:

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

对于 www 到非 www,使用:

For www to non-www, use:

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

如果您使用的是 Windows IIS 服务器,则上述代码对您不起作用.如果需要,我可以分享程序.

If you're on a Windows IIS server, the above code won't work for you. I can share the procedure if need be.