How to Install WordPress in a Subdirectory

Website Administrators usually install their sites in a subdirectory when they use Content Management Systems. This is a good practice because in case they want to move their website from one host to another, the files are kept in one folder. If they also want to change their CMS in some future time, for whatever reason, they can install the new CMS in another directory and the files from both CMS’s will not get mixed up. This tutorial will teach you how to install WordPress in a subdirectory instead of the root director of your web host.

If you follow the steps below, remember to use the actual domain name of your website and the name of your WordPress installation folder instead of the words example.com and subdirectory, respectively.

1. Install WordPress in a subdirectory

Refer to the article How to Install WordPress via Softaculous.

2. Update WordPress Address and Site Address

Type example.com/subdirectory/wp-admin in your browser and log in to your WordPress website. Go to Settings > General.

WordPress General Settings

Make sure WordPress Address (URL) is set to https://example.com/subdirectory. See to it that Site Address (URL) is set to https://example.com.

3. Check the root directory .htaccess file

Check the root directory of your web host and see if a .htaccess file exists in it. If it does, open it and see if the following code is written in it; write this in it if it is not written. Create the .htaccess file if it does not exist and write this code in it.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.php [L] 
</IfModule>
4. Check the installation subdirectory .htaccess file

Check the subdirectory where you installed WordPress and see if a .htaccess file exists in it. If it does, open it and see if the following code is written in it; write this in it if it is not written. Create the .htaccess file if it does not exist and write this code in it.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
5. Create an index.php file in the root directory

Create an index.php file in the root directory of your web host. Write the bits of code below in it:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require(dirname(__FILE__) . '/subdirectory/wp-blog-header.php');
6. Logging in to the site

You can log in as Administrator to your WordPress-powered website at example.com/subdirectory/wp-admin.