Most of us are familiar with files, directories, and subdirectories.   In the art of computer science, directories are a way to organize files into a meaningful hierarchy.  WordPress relies on hierarchical file systems to organize the thousands of required files in a WordPress instance.

History lesson! Hierarchical file systems were introduced in Microsoft’s world with DOS 2.0!

When installing WordPress, it is reasonable to place the installation itself into a subdirectory instead of in the primary web accessible directory (often called “./public_html/”). It is easier to manage the WordPress installation if installed in a simple subdirectory such as “/wp/”.  Ease of maintenance is especially important when you are faced with something as drastic as a reinstallation.  It is also just a whole lot cleaner, and you can even install multiple WordPress instances on your domain this way.

Steps involved with deploying in a Sub-directory

Deploying WordPress in a sub-directory is straightforward.  Follow these steps for success:

  1. First, install WordPress itself in a sub-directory
  2. Second, modify the .htaccess file in the primary html directory
  3. Third, modify the index.php file in the primary html directory
  4. And finally, change the WordPress Site Address

Install WordPress in Sub-directory

First thing is to install WordPress itself in a sub-directory.  Let’s say you choose “wp” as your WordPress installation directory.  Once you’ve completed your installation, users who visit your main page URL will likely wind up with a directory listing, something like the Index Of image below.

Of course, that isn’t what your users are looking for, and not what you wish to present to them either.  You want your users to land on your WordPress instance.  Let’s take care of that.

Modify .htaccess

Next, update the host environment to point to the WordPress installation.  The hidden file .htaccess (note the dot (.) in front of the file .htaccess) is used to redirect the landing page. Before starting this step confirm that WordPress is already installed in it’s own private directory (wp in our example) as outlined in the first step above.   

Edit the .htaccess file (don’t forget the leading period) in your root domain directory (/) by adding the following:

While you are working in the .htaccess file, I’d suggest making all requests convert to HTTPS if you have a certificate installed. If you don’t have a certificate installed, do it!

Edit the .htaccess file (don’t forget the leading period) in your root domain directory (/) with the following code which will accomplish both tasks:

# BEGIN WordPress - host from subdirectory modifications
# This strips off any subdirectories
<IfModule mod_rewrite.c> 
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress - host from subdirectory modifications

# BEGIN Convert *:// to https://
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END Convert *:// to https://

Update index.php

Next thing, copy the /index.php file from your working directory to the root directory. It is going to look something like this example. Then Add the /wp/ subdirectory to the appropriate ‘require’ line.

Copy the ./wp/index.php file to ./index.php, and update the subdirectory location for wp-blog-header.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__ ) . '/wp/wp-blog-header.php' );

Notify WordPress engine of change

Before we take this step, go to your URL.  The URL has been modified by .htaccess to include the subdirectory in the URL itself.  This is not likely what you wanted.  The last step is to clean up your URL.

Log into WordPress, go to the following:
WordPress >> Settings >> General

You will notice two URLs,

  • WordPress Address (URL),
  • Site Address (URL),

Change the Site Address to (that is, remove any sub-directory information listed). Leave the WordPress address exactly as it is, with the subdirectory attached.


You are all done!  Happy WordPressing!

If you have any questions or comments on this article please leave me a comment by clicking here.

Reference documents

  2. “Changing The Site URL”,
  4. “Apache Module mod_setenvif”,
  5. “RewriteRule Flags”,
  6. “Mod_Rewrite Variables Cheatsheet”,
  7. “How to Force HTTPS Using the .htaccess”,

Leave a comment