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.
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?
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.
Thanks
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"
- Install the ssl certificate (follow the instructions give to you by
the issuer)
- 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.
- 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.
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.
- Install this plugin Easy HTTPS Redirection and activate. This plugin will redirect all traffic from http to https regardless of what; you can also define the pages you don't want forced to https.
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.
- Repeat step 1 and 2 above.
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
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]
or any of the various approaches given at
http://www.askapache.com/htaccess/http-https-rewriterule-redirect.html
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();
}
}
Until this point with both methods https should be working well.
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]
For www to non-www, use:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
If you're on a Windows IIS server, the above code won't work for you. I can share the procedure if need be.