WordPress: Configure WP to work from Subdirectory

So you’ve decided on WordPress as your Content Management System.  Yay for you!  WordPress is a great choice.  This article will help you organize your directories & files, clean up the root file system, and at the same time help you help your users navigate your site from the root domain URL.

Install WordPress in Sub-directory

As you initiate your WordPress installation, be sure to create a sub-directory for the WordPress installation.  There is no need to litter your home directory with a bunch of WordPress files.  After all, that was the whole idea behind hierarchical file systems first introduced in MSDOS 2.0.

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

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.  Before starting this step, confirm that WordPress is already installed in it’s own private directory (wp in this 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:
    # 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]
    # END WordPress

Create index.php

Next thing, create the /index.php file in the root URL directory

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

Clean up Wordpress

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,

  1. WordPress Address (URL).
  2. Site Address (URL).

Change the Site Address to http://www.yourdomain.com (that is, remove any sub-directory information listed).


You are all done!

Happy pressing!

Reference documents

  1. http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory#Pointing_your_home_site.27s_URL_to_a_subdirectory
  2. 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 Reply

Your email address will not be published. Required fields are marked *