WordPress Migration – Broken Image Links

This will be a short post, just wanted to highlight an issue I faced last week with a WordPress migration. This is when you move a WordPress site over to a new server. There are several different ways of doing this, and also some plugins that work quite well. One to consider is Updraft, which is also excellent for backing up your site.

How to Migrate a site

However the way I did it was a more manual method. First step is to log into the server using an SSH client, Putty on Windows is good for this. Then you make a backup of the database .You then connect to the server using your FTP software of choice, in my case this is always Filezilla.

At this point you can do a full download of all the files , images etc.. including the database backup you just made

Once you have access to the new server there are a number of steps to take, depending on your server configuration. WordPress requires a number of PHP extensions to run, there is a detailed guide for installing on Ubuntu 20.04 here.

Once this is done, you can then upload all the files , and import the database. This can be done via the command line, through Putty, or with a Mysql client, table plus is very good and free for basic use cases. At this point you have all the files and the database, but you are not yet done! Once apache is configured (or Nginx), if you try and browse to your site it won’t yet work. You have to go into the wp-config.php file and enter the database configuration parameters if they have changed (the may not have). You also have to make sure the permissions are set correctly on all the files, this is a common problem. Finally there are two entries in the database that must be changed , or else you will get constant redirects to the old site. These are the first two lines in the wp_options table, which are siteurl and home. Both need to be set to the new site address

Broken Image links

Your site will now (most likely) work fine. However , the images may be missing. This is because when you upload an image to WordPress, two things happen. The image itself is uploaded to an uploads folder, but the address of that folder is written to the database, and this will be the old site address, not the new one. So although the images are actually present, and you will see them in the gallery, they will not load on the live site. The fix is simple. Log into the database and run the following code.

UPDATE wp_posts SET post_content=(REPLACE (post_content, ‘oldsite.com’,’newsite.com’));

This will update all the posts with image links, so that the old site address is replaced with the new one in every post.

And that should be that!