How to replace old domain to new domain in WordPress database

Post Reply
User avatar
isscbta
Team Member
Posts: 130
Joined: Mon Jul 19, 2021 1:41 am
Has thanked: 15 times
Been thanked: 3 times

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"

Example of replacing non-www to www:

Code: Select all

cd /home/someMyVestaUser/web/domain.com/public_html/
USER='someVestaUser'
FROM_DOMAIN='https://domain.com'
TO_DOMAIN='https://www.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"

FROM_DOMAIN='http://domain.com'
TO_DOMAIN='http://www.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"

Example of replacing www to non-www:

Code: Select all

cd /home/someMyVestaUser/web/domain.com/public_html/
USER='someVestaUser'
FROM_DOMAIN='www.domain.com'
TO_DOMAIN='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"

Tags:
Post Reply