MENU

by • 21/05/2016 • DevelopmentComments (0)262

Moving a WordPress Multisite to HTTPS

Disclaimer: This was done on a Multisite installation that uses the sub-directories and not sub-domains. Sub-domains is another beast and would need another method. Maybe I will write something about that in the future.

Disclaimer 2: I will not be held responsible for any losses (data or otherwise) as a result of this method. Backup your sites before you start this folks!

Step 1:

First make sure that you have your SSL certificate installed on your server. In most cases you can check this easily by going to your site and changing the http at the front of the domain name to https. If your page loads and your browser shows a little green lock (that says that your connection to the site is secure) you should be good to go. If you’re unsure, check with your hosting company. They will be able to assist.

Untitled-1

Step 2:

Now that we’re sure your SSL certificate is installed we will test this by switching your admin area over to https. This is very easy to do and is also a good check to confirm that your certificate is correctly installed on the server.

In your wp-config.php file, add the following code just above the line that says “/* That’s all, stop editing! Happy blogging. */”, and upload to your server:

define('FORCE_SSL_ADMIN', true);

Once done, you will need to log in again. You should now be able to navigate your entire admin area without any issues all while using HTTPS (with the little green lock).

If not, simply remove the line from your wp-config.php file and contact your hosting company to install the certificate properly.

Step 3:

Once step 2 is working perfectly we can continue to the database part of the process. Please make sure to backup your database before doing this!

You might want to do this at night when nobody will be making changes to their respective sites as any changes made in this period could go missing as a result of what you’re doing.

The way I did this was as as follows. I created a new database on the host with a name that makes it clear which one you’re accessing by adding a “_https” on the end of the name. So if your database name is “mysuperdb”, create another database called “mysuperdb_https”. Now export your data from your main database and import it into the new one.

Once done, switch your database name in your wp-config.php to the new one. Your site should be running exactly the same as before at this point. If it doesn’t you will need to switch back the database name in your wp-config.php file and do the export and import thing again.

The reason for the second database is that if you should mess up the next steps you can quickly switch back to your previous database and minimize down time.

Step 4:

OK, only continue with this step if all of the above steps were successful!

WordPress doesn’t currently have an easy way to do this, and they say you need to manually replace the values in your database which is simply unrealistic if you are operating larger network. So.. I found a script online to help with this.

We are now going to run a search and replace through the new database to switch all instances of “http://www.yoursite.com” to “https://www.yoursite.com”.

Copy the following PHP script into a new file named replace.php and replace the values in the top part of the code. If you’re unsure of these values check what you have in your wp-config.php file. For the database name use the newly created database name (with the “_https” at the end), as specified in step 3.

You will also need to replace the $find and $replace values with your site’s base URL, excluding trailing slashes.

<?php
 // Connect to your MySQL database.
 $hostname = "localhost";
 $username = "databaseusername";
 $password = "databasepassword";
 $database = "databasename_https";

 mysql_connect($hostname, $username, $password);

 // The find and replace strings.
 $find = "http://www.yoursite.com";
 $replace = "https://www.yoursite.com";

 $loop = mysql_query("
 SELECT
 concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''{$find}'', ''{$replace}'');') AS s
 FROM
 information_schema.columns
 WHERE
 table_schema = '{$database}'")
 or die ('Cant loop through dbfields: ' . mysql_error());

 while ($query = mysql_fetch_assoc($loop))
 {
 mysql_query($query['s']);
 }
?>

Once done,  you can copy this file up to your server, to the same directory as your wp-config.php.

Now you can direct your browser to the following link: http://www.yoursite.com/replace.php and hit enter once. Also don’t refresh, this needs to run once only!

Step 5:

That’s it, you should be done now. When you access your site now you will see that it’s all running from HTTPS.

Click through your entire site and some of the multisite sites as well. Also log into your admin area and make sure you can access your Dashboard as well as some of you network sites’ dashboards.

If everything is working fine you can delete the replace.php file from your server.

This entire process took me about 20 to 30 minutes on a network of about 70 sites. So it’s quite fast. 🙂

Let me know how it worked for you.

happy wheels

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *