WordPress: Configure WordPress to work from a Sub-directory

Most of us are familiar with files, directories, and sub-directories.   In the art of computer science (yes, that “art” v “science” reference was intentional), directories and sub-directories are a way to organize files into a meaningful hierarchy.  WordPress installations rely on this hierarchical file organization to make sense of the thousands of files that are required to make WordPress work.

History lesson! Hierarchical file systems were introduced into the land of Microsoft with DOS 2.0!

When installing WordPress, it is a reasonable idea to place the installation itself into a sub-directory instead of in the primary web accessible directory (which is often called “./public_html/”).  It is easier to manage the WordPress installation if installed in a simple sub-directory such as “/wp/”.  Ease of maintenance within a sub-directory is especially true when the system is having problems and you are faced with something as drastic as a re-installation.  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.

Directory structure with WordPress installed in subdirectory
Directory structure with WordPress installed in subdirectory Directory structure with WordPress installed in subdirectory

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, we’ll update your home directory itself to point to the WordPress installation you’ve just completed.  This is accomplished by way of updating the .htaccess file (note the dot (.) in front of the file .htaccess).  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.  Here are the steps to redirect to the sub directory.  

  1. 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! The following code changes all requests to https://

# 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]
</IfModule>
# 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]
</IfModule>
# 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.

2. Copy working /wp/index.php to ./index.php, and update the subdirectory location for wp-blog-header.php.

<?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' );

Tell WordPress to masquerade as the root directory

Before we take this step, go to your URL.  You should notice the WordPress instance, but if you look at the URL it has been modified to include the sub-directory in the URL itself.  Not likely what you wanted.  The last step is to clean up your URL.

Log into your WordPress instance, go to the following
WordPress >> Settings >> General

You will notice two URLs,

  • WordPress Address (URL).
  • Site Address (URL).

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

Congratulations!

You are all done!  Happy WordPressing!

Reference documents

  1. http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory#Pointing_your_home_site.27s_URL_to_a_subdirectory
  2. “Changing The Site URL”, http://codex.wordpress.org/Changing_The_Site_URL
  3. https://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet/#HTTPS
  4. “Apache Module mod_setenvif”,  http://httpd.apache.org/docs/current/mod/mod_setenvif.html#setenvif
  5. “RewriteRule Flags”,  http://httpd.apache.org/docs/current/rewrite/flags.html#flag_e
  6. “Mod_Rewrite Variables Cheatsheet”,  https://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet/
  7. “How to Force HTTPS Using the .htaccess”, https://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file

Leave a comment