Using wp-admin:
The most common method is to change your WordPress URL directly from the admin dashboard. In the admin menu, go to Settings > General to access the general settings screen. You can then update the following:
- WordPress Address (URL): The address to reach your site.
- Site Address (URL): The address of your WordPress core files.
After saving the changes the new url will be saved into the database, and your website accessible via the new url.
Using PHPMyAdmin:
The second method you can use to change your WordPress URL is directly in the WordPress database. You can either export the database and make changes or use a tool like PHPMyAdmin to update the HOME and SITE URLs.
From your cPanel open the PHPMyAdmin:
Click your database in the left panel. Several tables appear below it. Click on your wp_options table:
Locate the siteurl and home rows. Click the pencil icon next to each to edit.
Alter the URL by typing in a new one.
Click the Go button to save the settings. Do this for both the ‘siteurl‘ and ‘home‘ options.
Using the wp-config.php file:
You can also change the URLs in your wp-config.php file. This is a little easier than editing your database and will give you immediate access to your site.
NOTE: Values in wp-config.php override the settings in your wp-admin screen.
From your cPanel hosting account go to File manager. If you do not have cPanel hosting, simply log via FTP or SSH to your server and navigate to the WordPress directory.
In your WordPress directory, edit the file named wp-config.php.
In that file, add the following lines just above /* That’s all, stop editing! Happy blogging. */.
define('WP_HOME','https://example.com'); define('WP_SITEURL','https://example.com');
If you visit your site now, you’ll see you can log in.
NOTE: The database did not update your HOME and SITE URL. You should now use a plugin to update all URLs in your database to the new URL.
Using MySQL replace command:
Create MySQL dump:
wp db export
or
mysqldump username_dbname > username_dbname.sql
Create backup of SQL file:
cp -rp username_dbname.sql{,-bk}
Run to replace:
replace 'OLDdomain.com' 'NEWdomain.com' -- username_dbname.sql
Import back the updated data:
mysql username_dbname < username_dbname.sql
Clear your browser’s cache/cookies and re-test your new URL.
Using vi/vim editor:
Open SQL dump file using vi editor
vi username_dbname.sql
Type as the following:
:%s/OLDdomain\.com/NEWdomain\.com/g
Press Enter on your keyboard to run replacement
To save changes and quit type as the following and press Enter:
:wq!
Depends on the size of your SQL dump file the saving can take some time, so be patient
Import updated data:
mysql username_dbname < username_dbname.sql
Clear your browser’s cache/cookies and re-test your new URL.
Using WP-CLI:
Another option is to use the WordPress Command Line Interface (WP-CLI) to access the site and change the url.
NOTE: WP-CLI is a command-line interface and therefore requires SSH access.
If you are unfamiliar with WP-CLI you can check out this in-depth post from Kinsta on managing WordPress from the terminal.
wp option update home 'http://yoursiteurl.com'
wp option update siteurl 'http://yoursiteurl.com'
Clear your browser’s cache/cookies and re-test your new URL.
Redirection After Changing WordPress URLs
NOTE: Even after you’ve changed the site url, uploads and hard-coded links are still present in your database & theme files. To fix that you should use some kind of redirection, for example add the following to your .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
Alternatively, you can use a plugin such as Redirection to set up a redirection from your old domain name to your new one.