How to replace old domain to new domain in WordPress database

Post Reply
User avatar
isscbta
Posts: 39
Joined: Mon Jul 19, 2021 1:41 am

In case you need to replace the domain on the WordPress site, there are few multiple scenarios you should follow in order to achieve this:

At the very beginning download the latest wp-cli

Code: Select all

wget -nv https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/local/bin/wp
chmod +x /usr/local/bin/wp
In your SSH, as root, run:

Code: Select all

cd /home/someMyVestaUser/web/new-domain.com/public_html/
USER='someVestaUser'
FROM_DOMAIN='old-domain.com'
TO_DOMAIN='new-domain.com'
sudo -H -u$USER wp search-replace "$FROM_DOMAIN" "$TO_DOMAIN" --precise --all-tables --skip-columns=guid
sudo -H -u$USER wp cache flush
grep -rl "$FROM_DOMAIN" ./ | xargs sed -i "s#$FROM_DOMAIN#$TO_DOMAIN#g"
In case you want to move from localhost plus to move it to HTTPS, then first run this (after this run the same just without HTTP:// and HTTPS://):

Code: Select all

cd /home/someMyVestaUser/web/new-domain.com/public_html/
USER='someVestaUser'
FROM_DOMAIN="http://localhost:8081"
TO_DOMAIN="https://new-domain.com"
sudo -H -u$USER wp search-replace "$FROM_DOMAIN" "$TO_DOMAIN" --precise --all-tables --skip-columns=guid
sudo -H -u$USER wp cache flush
grep -rl "$FROM_DOMAIN" ./ | xargs sed -i "s#$FROM_DOMAIN#$TO_DOMAIN#g"
In case old site was in subfolder but you want to put site in root of domain, first replace "/subfolder" to "" (empty string) in .htaccess file, then run:

Code: Select all

cd /home/someMyVestaUser/web/new-domain.com/public_html/
USER='someVestaUser'
FROM_DOMAIN='old-domain.com/subdomain'
TO_DOMAIN='new-domain.com'
sudo -H -u$USER wp search-replace "$FROM_DOMAIN" "$TO_DOMAIN" --precise --all-tables --skip-columns=guid
sudo -H -u$USER wp cache flush
grep -rl "$FROM_DOMAIN" ./ | xargs sed -i "s#$FROM_DOMAIN#$TO_DOMAIN#g"

Some plugins put backslashes in URLs, so if something is still pointing to the old domain, then try this search:

Code: Select all

cd /home/someMyVestaUser/web/new-domain.com/public_html/
USER='someVestaUser'
FROM_DOMAIN="http:\\/\\/old-domain.com"
TO_DOMAIN="http:\\/\\/new-domain.com"
sudo -H -u$USER wp search-replace "$FROM_DOMAIN" "$TO_DOMAIN" --precise --all-tables --skip-columns=guid
sudo -H -u$USER wp cache flush
grep -rl "$FROM_DOMAIN" ./ | xargs sed -i "s#$FROM_DOMAIN#$TO_DOMAIN#g"
In case it was in a subfolder:

Code: Select all

FROM_DOMAIN="http:\\/\\/old-domain.com\\/subfolder"
TO_DOMAIN="http:\\/\\/new-domain.com"

Tags:
Post Reply