At some point during the development of your website you are going to need to move it to a different server. You may start out working on your local machine using MAMP or XAMPP or you may work on a staging server before pushing your site live. Someday, you may even need to change the URL of your website altogether.
Now, you are working with WordPress and you know that WordPress stores the Site URL and Blog URL in the database and uses this to provide the URL to the enqueued CSS and JS files and for your menu, among other things. You go and change this, but you see that there are other URLs in your content or plugins that have not been changed. How can you be sure that all of your URLs are changed without having to sift through each and every page and post or having to install yet another plugin?
Enter the Search Replace DB PHP Script. This is a PHP script that uses your WordPress wp-config file to gather your database information then searches your database for any string of text you’d like and replaces with another string of text. This can be done to replace any information into the database, however, it is extremely useful for replacing all of the URLs on your site.
Step 1 – Upload the PHP File
The first step in the process is to grab the PHP file from the GitHub repo and upload it to your website. To follow my instructions you will need to grab the searchreplacedb2.php file (They also have a file that can be run from the command line. I will be running the file through our web browser). Upload this file to the root directory of your website so it can be found at http://www.sitename.com/searchreplacedb2.php (of course you’ll need to replace www.sitename.com with your own site’s URL).
Step 2 – Begin Running Script
Now, you will need to run the script by visiting the link I just talked about: http://www.sitename.com/searchreplacedb2.php
You will first be presented with a checkbox asking if you want to pre-populate the DB values with the ones included in your wp-config.php file. To make it easy, you’re going to want to do this. If, for some reason this won’t populate or your server configuration doesn’t support his you can enter your database connection info at the next step.
Step 3 – Database Details
These database details will be pre-populated with the correct information if the above step worked properly. Don’t worry, if it didn’t find the correct info, you can do it manually too. You will need to get the information in the graphic below. This can all be found by opening your wp-config.php file in a text editor or you should be able to get it by contacting your hosting provider.
Step 4 – Select Database Tables
Next you will be given an option to select which database tables you would like to perform your replacement. For our purposes, you won’t need to make any changes. Changes would need to be made in other instances where you need to replace a text string, but limit it to only certain areas of your website’s database. For our purpose, we will be leaving the Leave GUID column unchanged box unchecked. If you are changing an existing site to a new URL rather than a development site, you may want to check this.
We are going to leave this screen unchanged and Continue. You will be presented with a warning asking if you are sure you have the correct tables before proceeding.
Step 5 – Replace Your Content
Now we are going to perform the actual replacement. In the first box, you will want to input the WordPress URL you want to be replaced. Enter this without the http:// or any trailing slashes or subdirectories (unless your staging site, local site, or live site are located at a subdirectory.
As shown in the example, I have a local development site set up at www.replacedsiteurl.dev and I want to replace this URL with our live URL at www.yournewsiteurl.com. This will take any instance anywhere in the database with the string ‘www.replacedsiteurl.dev’ and replace it with ‘www.yournewsiteurl.com’. Please note, that if you are using this to replace a once live URL, you would also replace any email address with the url in it too. For instance, if you replace the URL ‘oldsite.com’ with the URL of ‘newsite.com’ then an email address such as firstname.lastname@example.org will become email@example.com.
Once you are sure you have the proper URLs in place click submit and the magic begins.
Step 6 – You’re Done!
Woohoo! Now you’re replaced your URLs. You will be presented with a success message as shown below. It will give you a few stats on how many things were replaced and remind you of one of the most important things… REMOVE THE SCRIPT!!! If you keep the script on your server, someone could easily come across it and replace ANYTHING in the database of your website. It goes without saying that this could really F#&% things up on your website. So, just rmember to REMOVE THE SCRIPT. All you have to do is FTP back to your site and remove the searchreplacedb2.php file.
Now all of the URLs for your site should be changed and you’re ready to view your site in it’s new location.
I hope you you have gotten something from this. I find this the easiest way for me to change URLs on my WordPress websites. I don’t like messing with unnecessary plugins for jobs that can be done easily like this. Other options are available, feel free to share yours in the comments. This is the way I use and have found to be the easiest and most effective for my workflow.