How to move WordPress to a subdomain

Our generally recommended setup when running a Static WordPress website is using a subdomain.

What that means is that you have:

  • WordPress is running on a subdomain (cms.mysite.com)
  • The static site running on your root domain (mysite.com

This ensures that you can limit access to WordPress entirely via Basic Auth while serving a super fast and secure website on your root domain.

To do that, there are only three steps involved in the process:

  • Create the subdomain and set an A record to the same IP as your root domain
  • Update the WordPress Address and Site Address of your website
  • Replace all instances of the old URL with the new URL

As we publish our static site to the root domain, you don't have to set up redirects, as all your content will be available under the same URL structure as before.

Our Example

For the purpose of this tutorial, we are using a domain called mystatic.site.

This domain is currently set to a WordPress installation, but our goal is to move it to cms.mystatic.site so we can use mystatic.site for our static website later.

Set up an A-Record

The exact steps may differ depending on where you manage your DNS. In this example, we are using Cloudflare to manage our DNS settings.

We set up an A-Record with the exact same IP address our current root domain is pointing to:

As you can see in the screenshot, we created an A-Record with the name cms and set it to the exact same IP as our root domain.

Change the URL in WordPress

Now that our domain points to the WordPress installation, we can change the URL within WordPress.

There are two ways to handle it:

WordPress Admin Area:

Go To Settings -> General and change WordPress Address (URL) and Site Address (URL):

Save the settings, and you will be logged out.

Now navigate to the admin URL of your subdomain - in our case, this would be cms.mystatic.site/wp-admin—and log in again.

Change via wp-config.php

If the settings mentioned above are blocked and can't be edited on your hosting environment, you can do the exact same thing by editing your wp-config.php file.

Add the following code to your wp-config.php file to set these values:

define( 'WP_HOME', 'https://cms.mystatic.site' );
define( 'WP_SITEURL', 'https://cms.mystatic.site' );

Please ensure to replace the URL with your own subdomain URL before saving and uploading the modified file again.

After logging in again, the result should look similar to this:

Replace all instances of the old URL with the new URL

Now that our website is running from the subdomain, we want to replace the old URL (mystatic.site) with the new URL (cms.mystatic.site) within our entire installation.

Changing the WordPress Address (URL) and Site Address (URL) will not automatically change all the URLs that are inside of posts, pages, attachments, and settings of other plugins and themes.

Go to Plugins -> Add New Plugin and search for Better Search and Replace.

Install it, activate it, and navigate to Tools -> Better Search and Replace:

Now add your old URL (mystatic.site) in the field below "Search For" and the new URL (cms.mystatic.site) in the field below "Replace With"

Select all the tables and click Run Search/Replace.

Don't worry - "Run as dry run" is automatically activated by default, so we ran a test replacement before making any real changes to your database.

Depending on the size of your website, that might take a moment.

Once you are happy with the results, disable the "Run as dry run" setting and re-run the process - this time, it will replace the URLs within your database.

Wrapping Up

You successfully moved your WordPress installation to a subdomain, amazing!

You can now delete the A-Record for your root domain and use it for your static website instead.