This post is from a developer for developers having, beyond others, a solid SQL knowledge and understanding. If you have the slightest doubt you might not be one of those, please do not follow the steps described below! Of course, if you know you are not a developer at all, please also do not follow this guide.
This post describes how I was able to move my blog. This does not mean it will work for you the same way. As environments and setups are different, there is not guarantee. Therefore, you use the information provided here at your own risk. Whatever you do, make sure you have created backups and are able to restore. The author is not responsible for any damage.
OK, now having all the warnings and stuff covered, let’s get to the main point.
I was faced with the challenge to move this blog, powered by WordPress, to a different URL with SSL enabled. Fortunately, I just had to change the URL, but was able to continue to use the existing WordPress installation and database.
The goal was to have the blog hosted by a SSL encrypted site, but also not to break any existing external link. Means, whatever other site link to the current address, these links should be redirected to the new site, including all parameters to ensure the user will be forwarded to the expected post. E.g. the external link is http://blog.instance-factory.com/?p=181, the user should be forwarded to https://instance-factory.com/?p=181.
Of course, this requires to still own the old domain. Otherwise, I will not be able to setup the permanent redirect.
Searching the Internet for how-to, I chose to use
https://wordpress.org/support/article/moving-wordpress/ as my primary source for the steps to take. I also had a look at https://www.wpbeginner.com/wp-tutorials/how-to-add-ssl-and-https-in-wordpress/, but I came to the conclusion that finally there is no difference in what needs to be done to just move to a different URL or to move and enable SSL/HTTPS in a single step.
Prepare the Certificate
This site is hosted by a service provide, where I only had to checkmark “create SSL certificate” and “always forward to HTTPS”, and the destination address was set up. No need to acquire a certificate and so forth. In case you are not in such a comfortable situation, make sure to have these things ready before you start moving your blog.
I decided to try to not use any additional plugins or third-party tools / scripts. I move my blog, so I want to have full control. And it turned out it was quite easy 🙂
Step by Step
The steps below were taken from the list WordPress support offers, extended with some additional steps I found useful.
- Create an interim page, telling the visitors of your site that it is currently moving to a different site and therefore not available at the moment.
Here is the one I was using: https://instance-factory.com/bloginterim/.
- Make this page the landing page of your current site.
- Download your existing site files.
- Export your WordPress database (and others, if required).
- Move the backed up files and database into a new folder – somewhere safe – this is your site backup.
- To ensure the restore is working, create a new database and restore the backup to it.
- Run the database statements required to change the URL within the WortPress data against this new database to verify they are working as expected (see below for scripts).
- Log in to the site (blog) you want to move and go to Settings > General, then change the URLs. (i.e. from http://example.com/ to http://example.net ) – save the settings and expect to see a 404 page or some other error telling you the browser is not able to reach an address.
- Download your site files (WordPress) again (to a different place than the first download, of course 😉 ).
- Export the database again (again: do not override the first backup).
- Edit wp-config.php with the new server’s MySQL database name, user and password (as I was just moving to a different URL, but was able to continue using the existing database, I didn’t had to perform this step).
- Upload the files (I skipped this step too, as the new URL is hosted by the same Web server).
- Import the database on the new server (I skipped this step too, as I was able to continue using the initial database).
- Change the values of database column guid in table wp_posts (see below for SQL scripts)
- Change the values of database column post_content in table wp_posts (see below for SQL scripts)
- Redirect the new site to the WordPress folder.
- Log in to the new site, check if the site is fully marked as secure. If not, check the custom settings of your theme if they contain links to non-HTTPS content. I had links to images defined, referring to the media library of my site. As I was not able to figure out where this data is kept (database, …), I just changed the custom theme settings.
- Redirect the previous site URL to the new URL.
- Verify that the redirect is working property, including direct links to posts.
- Change all WordPerfect user passwords, as they were transferred as plain text in the past.
How to setup Permanent Redirect
For me, it was easy to change this on my environment. I just had to configure an external redirect and set the type to permanent (301) via the service provider’s UI for the old domain.
In case you don’t have such an easy way to create it, search the internet for “301 permanent redirect”. You should be able to easily find what suits best for your environment.
When setting up the permanent redirect, make sure parameters will also be passed.
Database Scripts to change the URL
As WordPress support mention in their guide, only data in the table wp_posts needs to be replaced. From what I saw when looking at the structure and content of the table, only two columns are affected: guid an post_content (at least at the time of writing). There is another column named post_content_filtered, but this only contained empty strings in my database.
Note: Please replace the my.oldaddress.com / my.newaddress.com placeholder in the scripts below with your actual values before executing them.
Changing guid Column
First step I took was to verify that each row contains the link to the old address in that column. So I counted the total number of rows in the table first:
select count(1) as total_rows FROM `wp_posts`
Then, I counted the rows having the old address included in column guid:
select count(1) as guid_count from wp_posts where guid like '%http://my.oldaddress.com%'
These two numbers should match. In case they don’t, please abort the move and analyse why not.
To replace the old address by the new one, you can use this statement:
update wp_posts set guid = replace(guid, 'http://my.oldaddress.com', 'https://my.newaddress.com') where guid like '%http://my.oldaddress.com%'
The number of affected rows must match to the value of guid_count. Again, if not, abort and analyse.
As a final check, you can verify that no row contains the old address in the guid column any more by re-running the statement determine guid_count.
Changing post_content Column
This is almost similar to the steps before. First count the number of rows that contain the old address in column post_content. Please note that here, the number is not expected to match to the total number of rows, as not all posts are expected to include a link to your blog site.
select count(1) as post_content_count from wp_posts where post_content like '%http://my.oldaddress.com%'
To replace the address, you can use this statement:
update wp_posts set post_content = replace(post_content, 'http://my.oldaddress.com', 'https://my.newaddress.com') where post_content like '%http://my.oldaddress.com%'
The number of affected rows must match to the value of post_content_count. Again, if not, abort and analyse.
As a final check, you can verify that no row contains the old address in the post_content column any more by re-running the statement determine post_content_count.
Please do understand that I will not be able to support you moving your site to a new URL.
But in case you have any feedback. please feel free to send an e-mail to
Moving WordPress by WordPress Support