How to replace old domain to new domain in WordPress database
Posted: Wed Aug 17, 2022 11:48 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
In your SSH, as root, run:
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://):
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:
Some plugins put backslashes in URLs, so if something is still pointing to the old domain, then try this search:
In case it was in a subfolder:
Example of replacing non-www to www:
Example of replacing www to non-www:
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
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"
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"
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"
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"