Installing WordPress in its own subdirectory

There are occasions when I prefer to house WordPress in its own subdirectory, such as when I’m using it as the core CMS but building auxiliary functionality alongside it with something like CakePHP.

Note: If you’re planning on using version control with your project, I’ve outlined a slightly different method for installing WordPress in a subdirectory that includes setting up a repository as well as relocating and manually adjusting the essential config files.

If version control’s not your thing, here’s how to do it (with a hat-tip to WordPress’s own tutorial):

  1. Download the most recent version of WordPress and install it in the root level of the project directory, completing the installation via browser window. For the purposes of these instructions, let’s assume the project directory is named new_project and the site URL is http://new_project.com.
  2. If you’re planning on using “pretty” permalinks (and why wouldn’t you?) create a file named .htaccess in the root level of the project directory (as simple as touch .htaccess from the commandline) then chmod so WordPress can write to it. Depending on your server setup this is usually 666. It’s okay to leave the file itself blank; there’s a reason why we’re giving WordPress write permissions.
  3. Log into the admin backend of the new WordPress installation and click Settings > Permalinks in the left navigation. Configure your permalinks structure then save changes. The message we’re looking for is Permalink structure updated. If you see You should update your .htaccess now the file permissions aren’t permissive enough. If you run into problems consult the WordPress guide to changing file permissions.
  4. Once WordPress has successfully updated your .htaccess file, click Settings > General in the left navigation. Append the URL in the WordPress Address (URL) field with the subdirectory name. In our example, http://new_project.com would become http://new_project.com/cms. Confirm the URL in the Site Address (URL) field remains unchanged (http://new_project.com in our example).
  5. Buckle up and save changes. There are going to be error messages and your brand new site will complete disappear but fear not — everything will be okay in a moment.
  6. Within the project directory create a new subdirectory for WordPress. Let’s name it cms (or, more accurately, new_project/cms/).
  7. Move all the WordPress core files from the root level of the project directory into this new subdirectory. Remember to include all the invisible files (like .htaccess).
  8. Copy index.php and .htaccess back to the root level. Don’t move these files; we need a copy of both in both the project directory and the WordPress subdirectory.
  9. Open the root-level index.php file in your favorite text editor and add the subdirectory to this line:
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );

    Like so (for our example):

    require( dirname( __FILE__ ) . '/cms/wp-blog-header.php' );
  10. Log into the admin backend. If everything is configured properly, the default admin login URL (http://new_project.com/wp-login.php in our example) should redirect properly to the subdirectory (http://new_project.com/cms/wp-login.php).
  11. Click Settings > Permalinks and save changes so WordPress will update the .htaccess file as needed. If you get any message other than Permalink structure updated go back to step #3 and adjust permissions on the root-level .htaccess file until you do.

Simple. Right?