Moving WordPress: Moving Your Site From a Subdirectory to the Root Directory
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 bemysite.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.
What You’ll Need
To follow this tutorial, you’ll need:
- an installation of WordPress that’s ready to go live
- an FTP client or CPanel File manager
- a code editor
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 You Start
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!
Getting Rid of the Old Site
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.
Removing a Static Site
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.
Removing a WordPress Site
If there’s an existing WordPress site in the root directory, you’ll need to remove it completely:
- Drop (delete) the old site’s database using phpMyAdmin. You’ll probably have two databases: one for the old site and one for the new. If you’re not sure which is which, check the wp-config.php file 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.
- Delete all of the WordPress files and folders in the root directory, being careful not to delete the folder your new site is in.
- Beware: don’t do this until you have made a backup!
Editing Your New Site’s Settings
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 atexample.com/development, change the settings as follows:
- WordPress address (URL): http://example.com/development
- Site Address (URL): http://example.com
Click the Save changes button and move on to the next steps before trying to access your site.
Editing and Copying Files
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.
Copying the Files
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 .htaccess file (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.
Edit the index.php file that you’ve moved. You could do this by:
- editing it in situ after the move, using an FTP client or cPanel file manager
- downloading it from the subdirectory, editing it and then uploading it to the root directory—instead of making a copy
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 index.php file.
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.