Most WordPress users install their sites in the domain root initially, but after a while, some may be worried to find that the root directory is cluttered up by numerous WordPress files. If this is your case, you may want to move WordPress to a subdirectory so as to keep the root organized and clean.
Thanks to the developers, you can install WordPress in a subdirectory but keep your website served from the domain root. Therefore, you are able to store the files in any folder you like without affecting the user experience.
In this post, we’d like to cover two topics: installing WordPress in a subdirectory when you set up a fresh website, and moving an existing WordPress install from the domain root to a subdirectory. No matter which situation you are in, your visitors can still visit your site with “http://yourdomain.com” when you are done. They will not even know that your files are stored in a subdirectory.
Note: To make sure a successful install/transfer, your web server (Apache) must have the mod_rewrite module enabled, and you have to get some basic knowledge about file permissions.
Run a Fresh WordPress Install in a Subdirectory
This task is quite easy if you have already attained the basic skills of installing WordPress and using the WordPress admin dashboard. Even if you don’t, there is no need to worry. Follow the steps below, and you will get everything done within half an hour.
Step 1: Install WordPress in a subdirectory
First of all, you need to install WordPress in a new subdirectory on your server. If you deal with the installation by using a script installer like Softaculous, you don’t have to create the subdirectory manually before the installation. Instead, you can simply enter the directory name during the software setup, and the creation will be handled by the installer automatically.
For example, in the case you enter “example” in the text field for directory, WordPress would be installed at “http://yourdomain.com/example“.
However, if you want to install WordPress manually, you will have to create the subdirectory on your server by yourself.
Need the detailed instructions for installing WordPress? Then read our comprehensive WordPress install guides.
Step 2: Create a .htaccess file
When you are done installing WordPress, log into your new WordPress site and navigate to Settings > Permalinks. Here you can select a proper permalink structure. During the changes, a .htaccess file will be generated automatically which you can use to harden your site security, set up redirections, etc. If the file already exists, it will be updated.
There are rare cases that neither of the situations discussed above occurs. If this is true, create a file named “htaccess.txt”, upload it to the subdirectory, and rename it as “.htaccess“. Then, set the file permissions to 644. At last, go to configure the permalink structure. Now the file should gain an automatic update. Or you have to contact your hosting provider to confirm the mod_rewrite module is enabled.
Step 3: Change WordPress Site Address
In the Settings > General section, you can see a number of general settings including the “WordPress Address” and “Site Address”. By default, the two fields come with the same value. Since WordPress has been installed in a subdirectory, the URL in both fields should be the location of the subdirectory.
However, to make your WordPress site served from the root directory, you have to change the “Site Address” to be your main domain while keeping the “WordPress Address” to the default. The correct values should be like what shown below.
Remember to save the changes. And you shouldn’t visit your website at this time because you will get some error messages. Don’t refresh your website until the following step is completed.
Step 4: Deal with index.php and .htaccess
Access your WordPress files by using cPanel or an FTP client like FileZilla. Locate the index.php file and .htaccess file in the subdirectory, and then copy and paste them to the root directory (the directory set up in “Site Address”). Pay attention that you should just copy the two files, and DO NOT move these files from the subdirectory.
If you cannot find the .htaccess file, it’s probable that the file is hidden and your file manager/FTP client isn’t configured to display hidden files. Simply make a small configuration to get the file visible.
After moving the files, open index.php file in the domain root in a text editor and find the following code.
Replace this line with the following one. Note that “example” should be the name of your own subdirectory.
After saving the file, you can access your site with “http://yourdomain.com” although the files are stored in a subdirectory instead of the root.
Move an Existing WordPress Site to a Subdirectory
If you want to clean your root directory by moving WordPress files to a subdirectory, follow the simple tutorial below.
Step 1: Make a complete backup of your WordPress site
It is always suggested to make a backup before making any significant change on your server because this keeps your site safe from all kinds of disasters. To prevent anything from going wrong, make sure you have at least one complete backup stored on the local PC or other storage devices.
Step 2: Create a subdirectory and change the WordPress Address
Since you will move WordPress to a subdirectory, the first thing you should do after backup, of course, is to create a subdirectory in the root directory with the use of cPanel or your FTP client. The name of the subdirectory can be whatever you want, and for this example, we use “/wordpress”. This directory will be used to store WordPress files.
After the subdirectory is created, log into your WordPress dashboard and go to the general settings under the Settings menu. Add the subdirectory at the end of the existing “WordPress Address” because your files will be served from this directory. Don’t change the “Site Address”.
Again, we would like to remind you not to visit your site right after saving the changes.
Step 3: Move WordPress files to the subdirectory and modify index.php
Now that your WordPress site is ready to be served from the new subdirectory which you enter in the “WordPress Address”, you can start moving all the WordPress files to this directory. This can be done easily either in the file manager of cPanel or in your FTP client. It’s just a simple copy-and-paste process.
After all the files are moved to the new location, copy index.php and .htaccess from the subdirectory and paste them in the root directory. Then, open index.php in text editor and locate the code below.
Modify this line to use the new directory of your WordPress files.
Step 4: Update the Permalink Structure
If your WordPress site has been set up to use pretty permalinks, you will need to update the permalink structure after moving the files.
Go to “http://yourdomain.com/wordpress/wp-admin” (“wordpress” refers to the name of the subdirectory) and log into WordPress dashboard with your credentials. Then, visit Settings > Permalinks to set up the permalink structure you like. During this process, the .htaccess file will be updated automatically. Even if you don’t make any change to the permalink structure, be sure to save the settings once more.
If your .htaccess file doesn’t come with the right settings for WordPress to write to it, some rewrite rules will be shown to you when you try to change the permalink settings. At this time, you need to copy the rewrite rules to the .htaccess file located in the root directory.
After all these steps are done, you can visit the frontend of your site to see that nothing has been changed. However, since the move of the WordPress files has impacts on the accessibility of your site, we suggest making the transfer in a midnight when you get the least visits.