Updating WordPress URLs in a database and testing
One of the last things on our ‘Go Live’ checklist is to repoint the domain to the server where the new site is. There are a few things you need to do before this can happen.
When making a WordPress site live you need to update the development URLs to the current URL. The easiest way to do this is by using a database script to run a find and replace on all the tables that could store the site address. The below can be used by swapping ‘oldsite’ and ‘newsite’ with the dev and live URLs respectively. If you have changed the default WordPress table prefix from ‘wp’ you will need to update that also. Whilst it is not always necessary, we recommend taking a backup of your database if you’re not that confident working with MySQL.
UPDATE wp_options SET option_value = replace(option_value, 'https://oldsite', 'https://newsite') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'https://oldsite', 'https://newsite'); UPDATE wp_posts SET post_content = replace(post_content, 'https://oldsite', 'https://newsite'); UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://oldsite', 'https://newsite');
If USING A SCRIPT seems too technical, there are lots of plugins out there that can help! OUR FAVOURITE IS GO LIVE UPDATE URLS.
Now that your database is using the new site URL you will not be able to view it from your dev link. Before we repoint the domain we need to ensure that our site is still working as expected with the new url. To do this we need to add the site URL to our host’s and redirect it to the development server’s IP. Here’s how…
For Mac OS
- Open your terminal.
- Make sure you’re in the home directory, cd ~.
- Open the hosts file for edit by running:
sudo nano /private/etc/hosts
-
It will then ask you for your password.
-
The file should have the below contents:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost
-
Making sure not to edit anything that is already in this file, add a new line with your server’s IP and the new url.
87.87.87.87 newurl www.newurl
-
Save the file (Ctrl-O)
For Microsoft
- Open Run Line and enter the below command. This will open the host file in notepad.
notepad c:\windows\system32\drivers\etc\hosts
- Now follow points 6 onwards from the ‘For Mac’ instructions.
You can now access the site from your browser and check that it is working as expected. Remember to remove the added line from your host files so that you can see when the DNS has been updated and propagated.
Check back soon for more TWK developer tips!