When and How to Install WordPress in a Subdirectory
When you manage multiple WordPress sites on the same web server, keeping your installs organized is a must, especially if you look after client sites. Or maybe you just don’t like installing WordPress at the root of your site. Whatever the case may be, setting up WordPress in a subdirectory provides a tidy solution.
In this post, we’re going to explore two scenarios: When you install WordPress for the first time and run the setup in a subdirectory, and when you need to move an existing installation from the root directory of your site to a sub-folder. For both scenarios, WordPress allows you to display your site as if it was installed in the domain root.
Note: The following examples have been tested on an Apache web server, as recommended at WordPress.org. Check if
mod_rewrite module is enabled and have a read over Understanding File Permissions if you need a refresher about on this topic.
Continue reading, or jump ahead using these links:
Installing WordPress in a Subdirectory
Download the most recent copy of WordPress and install it in a subfolder of your choice on your web server. If you need some help installing WordPress, check out our post A Guide to the Best Ways to Install WordPress. For this example, I installed WordPress in the /wp_install folder on my web server.
Once you’re done setting up WordPress, go to Settings > Permalinks screen and set your favorite Pretty Permalink structure. This will generate a .htaccess file, a directory-level configuration file used for site access issues, such as URL redirection and URL shortening. Sometimes WordPress doesn’t automatically generate or update your .htaccess file. In this case, you should check if WordPress has write access. You can read up on how to change file permissions in the WordPress Codex. The 644 settings is recommended.Now go to
Now, go to Settings > General in the backend of your WordPress sites and look for the WordPress Address (URL) and Site Address (URL) fields. WordPress Address is the address of the folder where you’ve installed WordPress, and the Site Address is your website’s URL.
Both addresses should always include http:// and never the final slash. By default, these fields have the same value.
Next, remove the /wp_install part from the Site Address (URL), so that:
- WordPress Address (URL) is http://example.com/wp_install
- Site Address (URL) is http://example.com
Save the settings you just edited, but don’t refresh your site just yet.
Now, using your preferred FTP client (mine is FileZilla), login to your site and copy the index.php and .htaccess files from your WordPress folder (/wp_install in this example) and paste them into the domain root (these files should always be together in the directory you set in the Site Address field – that’s the root in our example). Be careful to copy and paste these files, not just move them from your WordPress directory.
If the .htaccess file is not visible, this might be because it doesn’t actually exist (i.e. pretty permalinks aren’t enabled) or because your FTP client doesn’t show hidden files. This may also occur if you’re working from a localhost environment and your computer recognizes .htaccess as a system file and hides it. Whatever the reason, you can force your FTP client to show hidden files, as illustrated in the image below:
Open the index.php in the root directory of your domain in a plain text editor and replace the following line of code:
with this line of code:
That’s all you need to do. Now your website will be served from the root directory of your web server.
Moving WordPress from the Domain Root to a Subfolder
If you decide to move an existing WordPress site from the domain root to a subfolder, a complete backup of your site is absolutely necessary because the following steps involve updating your database tables. If something goes wrong while moving files and updating tables, it’s better to be safe than sorry.
If you need some tips on the best ways to backup your site, check out How to Backup Your WordPress Website (and Multisite) Using Snapshot and 4 Top WordPress Multisite Backup Solutions Tested and Reviewed.
Next, using your favorite FTP client, create a new folder on your web server, i.e. /wp_install or whatever you like.
Then you need to login to the backend of your site and go to Settings > General and change the URL values. Set WordPress Address to the folder URL and Site Address to the main site URL, i.e. the same values as the example earlier in this post.
Next, going back to your FTP client, move the whole WordPress install from the root directory to the /wp_install folder you created, keeping a copy of the index.php and .htaccess files in the root. Then open the root index.php file and replace the following line:
with this line of code:
With these changes in place, WordPress will look for wp-blog-header.php in its new location.
Now, go to http://example.com/wp_install/wp-admin/ (replacing “example.com” with your own domain, of course) and log in to the backend of your site. Go to Settings > Permalinks and set up your preferred pretty permalinks structure. Note that you may be required to save the permalink settings even if you do not make any changes to the permalink structure.
Lastly, it’s time to review the internal links in your navigation menus and post content. You could easily change the menu links from the Appearance > Menus page but it would be a little tricky changing links in existing posts. You could replace each occurrence of the old URL by running a query from your database or, better still, you could use a plugin to execute the same query.
Using the Better Search Replace plugin, you can comfortably search and replace strings in WordPress. Just go to Tools > Better Search Replace in the backend of your site once you’re installed and activated the plugin.
Our task is accomplished! Visitors to your site won’t see any difference and will access your website from the domain root.Tags: