WordPress: Redirect site to 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, and help you help your users navigate your site from the root domain URL.

Install WordPress in Sub-directory

First this first.  As you complete your first 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.

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.

Index Of directory listing after WordPress installation
Index Of directory listing after WordPress installation

Of course, that isn’t what you are looking for.  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 to point to WordPress installation

  1. WordPress is already installed in it’s own private directory (wp in this example).
  2. 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]
    </IfModule>
    # END WordPress

Create index.php

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

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

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).

Congratulations!

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

Leave a Reply