WP-CLI simplifies the administration of your websites, especially if you prefer to work on the command line.
For example, the search-replace command will look for a string (which you specify) and replace it with another (which you specify). This command can have a lot of useful applications, such as changing the URL/domain, updating the path of a file or even replacing each http:// call to https:// throughout the database.
Follow the instructions below using the search-replace command to replace an old URL with a new URL and to replace all http:// calls with https://
! We recommend that you perform a backup of your website before taking the necessary steps. Although it is unlikely that changes made with the search-replace command may cause your website to behave unexpectedly or even corrupt database information. Creating a backup BEFORE making changes will ensure the possibility of restoration in case of unexpected results.
Replacing an old URL with a new URL
If you want to change your domain, you can use the WP-CLI search-replace command to replace the URL configured for your website. For example, if you created a website at http://new.domain.com, you can use this command to replace the database login information to use http://domain.ro
- Log in to the server via SSH with the cPanel user who owns the website for which you want to change the URL
- Use the cd command to access the root folder of your website eg:
cd public_html/wp
Replace public_html/wp with the actual path to the root folder of your website.
Execute the following command:
wp search-replace {old URL} {new URL} –dry-run
Replace {old URL} {new URL} with the URL you want to find, or the URL you want to replace.
The output will display the details, including tables and number of replacements, as in the example below:
[email protected] [~/public_html]# wp search-replace http://example.com http://new.example.com --dry-run +------------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +------------------+-----------------------+--------------+------+ | wpcli_commentmeta | meta_key | 0 | SQL |
- After checking the replacements to be made, you can proceed with running the following command to update the database accordingly:
wp search-replace http://example.com http://new.example.com
The command will return the following output:
[email protected] [~/public_html]# wp search-replace http://example.com http://new.example.com +------------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +------------------+-----------------------+--------------+------+ | wpcli_commentmeta | meta_key | 0 | SQL |
- After checking the replacements to be made, you can proceed with running the following command to update the database accordingly:
wp search-replace http://example.com http://new.example.com
The command will return the following output:
[email protected] [~/public_html]# wp search-replace http://example.com http://new.example.com +------------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +------------------+-----------------------+--------------+------+ | wpcli_commentmeta | meta_key | 0 | SQL |
Replacing HTTP:// with HTTPS://
In addition to installing an SSL certificate, you may need to update your http:// calls to use the secure https:// version, which will help eliminate security warnings in browsers that can detect your website's upload resources via http:// instead of the secure https:// protocol. You can use the WP-CLI search-replace command to easily replace http:// with https:// by following the steps below:
- Log in to the server via SSH with the cPanel user who owns the website for which you want to change the URL
- Use the cd command to access the root folder of your website eg:
cd public_html/my_website
Replace public_html/wp with the actual path to the root folder of your website.
Execute the following command:
wp search-replace {http://URL} {https://URL} –dry-run
Replace {http://URL} {https://URL} with the URLs you want to find and replace.
The output will display the details, including the tables and the number of replacements made, as in the example below:
[email protected] [~/public_html]# wp search-replace http://example.com https://example.com --dry-run +---------------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +---------------------+-----------------------+--------------+------+ | wpcli_commentmeta | meta_key | 0 | SQL |
After checking the replacements to be made, you can proceed with running the following command to update the database accordingly:
wp search-replace http://example.com https://example.com
The following output will be returned:
[email protected] [~/public_html]# wp search-replace http://example.com https://example.com +---------------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +---------------------+-----------------------+--------------+------+ | wpcli_commentmeta | meta_key | 0 | SQL |
For assistance in applying the above instructions on HostX servers please contact us. HostX offers the fastest WordPress hosting and can help you solve your problems.
