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

Copy the ./wp/index.php file 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' );

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), http://www.yourdomain.com/wp
  • Site Address (URL), http://www.yourdomain.com/wp

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!

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

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