Changing the Primary Domain of a Site

There may come a time where you wish to change the primary domain that your site is accessible on. Although we don’t provide a method to do this via the app, it’s possible by manually performing the following steps.

Before continuing, please be aware that modifying the primary domain can negatively impact SEO.

Create a New Site

Start by creating a new site in SpinupWP. When prompted to enter the domain, enter the new primary domain you wish to use. On the ‘Database’ tab, select ‘Use Existing’ and select the database which your current site uses.

Create a new site

Copy Files

Next, we’re going to copy any existing files to the new domain. This will include your plugins, themes and uploads directory. SSH to your server using a sudo user and remove any files from the newly deployed site:

sudo rm -fr /sites/newdomain.com/files

Then, copy the files from the existing domain:

sudo cp -r /sites/previousdomain.com/files /sites/newdomain.com

As per File Ownership and Permissions the copied files and folders should be owned by the site user.

Find and Replace

Your site should now be accessible on the new primary domain. However, there may still be traces of the old domain in the database. To fix this you could use WP Migrate DB Pro or WP CLI to perform a find and replace on the database:

sudo -u {new_site_user} wp search-replace '//previousdomain.com' '//newdomain.com' --skip-columns=guid

Once the find and replace is complete, you’ll also need to flush the object cache:

sudo -u {new_site_user} wp cache flush

Redirect

The final step is to redirect the old domain to the new domain. If your site is running on HTTPS you will first need to add the old domain as an Alternate Domain to the new primary domain. This will ensure SpinupWP continues to generate a HTTPS certificate for the old domain, which will allow HTTPS traffic to be redirected.

Next, SSH to your server using a sudo user and create a new file within /etc/nginx/sites-available/newdomain.com/before/ called change-domain.conf with the following contents:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name previousdomain.com;

    ssl_certificate /etc/letsencrypt/live/newdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/newdomain.com/privkey.pem;

    return 301 $scheme://newdomain.com$request_uri;
}

If your site isn’t running on HTTPS you should remove both the listen 443 directives, and the ssl_certificate and ssl_certificate_key directives. Remember to update previousdomain.com and newdomain.com to reflect your domains.

Final Steps: Test & Verify

Test that the configuration is correct using sudo nginx -t. You can ignore any warnings like “nginx: [warn] conflicting server name”. If everything looks good, reload Nginx:

sudo service nginx reload

Test that your site works by visiting the old domain. If everything is configured correctly it should redirect to the new primary domain.

Ready to give SpinupWP a try?

Not sure if SpinupWP is the right fit for you? Send us an email at sudo@spinupwp.com and we’ll give you our no-BS opinion.