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_projectand the site URL ishttp://new_project.com. - If you’re planning on using “pretty” permalinks (and why wouldn’t you?) create a file named
.htaccessin the root level of the project directory (as simple astouch .htaccessfrom 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 > Permalinksin 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 nowthe 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
.htaccessfile, clickSettings > Generalin the left navigation. Append the URL in the WordPress Address (URL) field with the subdirectory name. In our example,http://new_project.comwould becomehttp://new_project.com/cms. Confirm the URL in the Site Address (URL) field remains unchanged (http://new_project.comin 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.phpand.htaccessback 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.phpfile 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.phpin our example) should redirect properly to the subdirectory (http://new_project.com/cms/wp-login.php). - Click
Settings > Permalinksand save changes so WordPress will update the.htaccessfile as needed. If you get any message other thanPermalink structure updatedgo back to step #3 and adjust permissions on the root-level.htaccessfile until you do.
Simple. Right?