Generally finding and replacing old URLs can easily be accomplished using WP-CLI’s search-replace
command. However that only makes changes to the database. Hard-coded file references may still exist. This can be handled with a few commands over SSH, however if done improperly you can easily break everything. In order to perform these replacements safely I recommend following these steps.
Step 1: Use grep
to locate the proper files to update
The following grep
command will recursively search through the entire wp-content
folder for files containing a specific search term.
grep -nr "http://sample-domain.tld" wp-content
You’ll want to weed out any returned files until you have just the files returned that you actually want to update. To narrow down the selection I recommend using the argument --include "*.php"
which will search for a specific file extension. You can hunt multiple file extensions at once by adding many include arguments like this --include "*.php" --include "*.css" --include "*.js"
. If your search is too generic then you might accidentally corrupt binary files like .pdf
which shouldn’t be modified directly.
Step 2: Feed the file list only from grep
to sed
You can see the file selections by adding the argument -l
to grep. This will list out all files which match a specific grep search. This list of files can then be feed into sed
to actually perform the text replacement. Again make sure you use the same grep arguments so that your modifications are only done on the files that need it.
grep -nrl "http://sample-domain.tld" wp-content | xargs sed -i '' -e 's#http://sample-domain.tld#https://sample-domain.tld#g'
Step 3: Verify references have been updated.
If all went well the original search grep -nr "http://sample-domain.tld" wp-content
should yield no results. Last use grep
to search for the new updates.
grep -nr "https://sample-domain.tld" wp-content