There are times when you need to create a WordPress site in a subdirectory and not the root directory for the domain it's hosted on. I sometimes do this if I'm developing a new WordPress site to replace a static site, and I want to leave the static site in place while giving the client access to the new site during development for approval. Even if you do your development work locally, you may sometimes need to do this.
When you come to make the site live, you don't want its URL to be mysite.com/subdirectory—you want it to be mysite.com. So you might think you have to manually move the site from its subdirectory to the site's root directory.
The good news is that you don't. You can leave the site exactly where it is and just tell WordPress what you've done by making a few changes to settings and minor edits to a couple of files. It just takes five minutes or less.
To follow this tutorial, you'll need:
This method will work on a standard installation of WordPress, and will work with most frameworks or if you're using a parent and child theme structure.
Beware! This method will not work for Multisite installations, which should always be in the root directory.
Before doing this, it's a good idea to make a backup of your site, just in case. Use your preferred backup plugin to do this. And if you haven't installed a backup plugin prior to going live, you really should!
Does your client have a horrible, outdated, static site that they've hired you to update and move to WordPress? Now's the time to consign it to history.
If there is an existing static site in the root directory, delete the files for it. I tend to make a backup locally just in case my client decides they need something from the old site, although this hasn't happened yet.
If there's an existing WordPress site in the root directory, you'll need to remove it completely:
wp-config.phpfile for the old site and it will tell you which database to drop. For advice on deleting a database, see this thread on Stack Overflow.
You'll need to edit two settings in your new site: permalinks and the site address.
Turn off pretty permalinks in the Permalinks screen, which you'll find in Settings > Permalinks. Do this by selecting the Default option and clicking Save Changes.
In Settings > General, change the address of your site but not the address of WordPress. For example, if you've been working on the site at
example.com/development, change the settings as follows:
Click the Save changes button and move on to the next steps before trying to access your site.
Before you can access your site, you'll need to make minor edits to a couple of files, so that WordPress knows where to find the site.
Using FTP or CPanel file manager, copy (don't move) the following files from your WordPress directory to the root directory:
.htaccess, if you have one. If there isn't an
.htaccessfile (and the fact that you've turned off pretty permalinks means you're less likely to have one), don't worry about creating one—just skip this step.
index.php file that you've moved. You could do this by:
The edit you need to make is to one line at the end of the file. Find the line that reads as follows:
Change it to:
So if you've been developing in
example.com/development, just change the line to:
Save the new
Back in the WordPress admin screens, turn pretty permalinks on again, with whatever settings you need for your site.
Visit the root domain of your site in the browser and it will display the site that's stored in the subdirectory, but won't show this in the URL, which will be displayed as the root URL. And that's it!
As you can see, moving WordPress from a subdirectory to the root directory is incredibly simple and doesn't actually require you to move WordPress. Just change some settings, copy and edit a couple of files, and you're good to go.