If you need a new url to be the primary one for your WordPress site, read on.
Edit the DNS Record of New Domain
Point the new domain by editing the A record for the domain to point to the IP of the server.
Create a New Virtual Host
Copy the original virtual host to create the new one and then edit it:
cp /etc/apache2/sites-available/original.com.conf /etc/apache2/sites-available/new.com.conf
nano /etc/apache2/sites-available/new.com.conf
Just update the domain information part, not the directory it points to.
Enable the new virtual host:
a2ensite new.com.conf
Restart apache2 to see the new domain in effect:
service apache2 restart
When you visit the website now, you’ll be able to visit with both domains. The original domain will show all files and images correctly, while the new domain the site will look a little broken.
Update the Database
You need to update the database to use the new domain for the wp_siteurl, wp_home and all links to files and images. There are two ways to do this.
- WP Migrate plugin
The quickest way to do this is to use the WP Migrate plugin to export the database. The plugin adds a Find and Replace already loaded with the current domain. Add the new domain in the Replace field beside it. You can remove the absolute path to the directory as that’s not changing. Export the database. - Edit SQL file with EditPad Lite
If you do not have WP Migrate installed, you can download the database and open the sql file with EditPad Lite which is good for opening large files. Do a find and replace with the new domain.
Drop all tables from the live website and then upload your new sql file from WP Migrate or your edited one from EditPad Lite into the database.
Now if you visit the new domain, the images will load correctly. You will also be able to see the original domain which we’ll need to change as only one should be primary.
Redirect to the Primary Domain
Update your htaccess file at the top to redirect all domains to the new domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^new\.com$ [NC]
RewriteRule ^(.*)$ https://new.com/$1 [R=301,L]
Now the domain is updated for your WordPress site!