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):
- 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 ishttp://new_project.com
. - 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 astouch .htaccess
from the commandline) then chmod so WordPress can write to it. Depending on your server setup this is usually666
. It’s okay to leave the file itself blank; there’s a reason why we’re giving WordPress write permissions. - 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 isPermalink structure updated
. If you seeYou 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. - Once WordPress has successfully updated your
.htaccess
file, clickSettings > 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 becomehttp://new_project.com/cms
. Confirm the URL in the Site Address (URL) field remains unchanged (http://new_project.com
in our example). - 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.
- Within the project directory create a new subdirectory for WordPress. Let’s name it
cms (
or, more accurately,new_project/cms/
). - 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
). - 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. - 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' );
- 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
). - Click
Settings > Permalinks
and save changes so WordPress will update the.htaccess
file as needed. If you get any message other thanPermalink structure updated
go back to step #3 and adjust permissions on the root-level.htaccess
file until you do.
Simple. Right?