Giving Wordpress its own directory
Wednesday, October 29th, 2008
Organization is important to me, so something I look for when installing third-party web applications is the ability to keep the application’s files cleanly separated from my own data and any additional files I’m using on the site. Sadly, most applications don’t support this very well but Wordpress is an exception. Wordpress provides the ability to keep your installation in a subdirectory out of the way of the rest of your site while allowing you to display your blog content at the root level. This keeps your web directory cleaner and reduces the risk of important files being modified or deleted accidentally.
This procedure is well documented on the Wordpress Codex but I’m writing it up here as well, as it’s a prerequisite to some other Wordpress techniques I’ll be discussing and would like to keep the instruction all in one place.
A note about terminology
In this, as well as all my tutorials, I use the domain name example.org to refer to your domain. When you see this, substitute your actual domain name. As well, in this article I will assume we’re installing Wordpress into a directory called /wordpress. If you use a different name for your Wordpress folder, just replace “wordpress” in any paths below with whatever name you chose.Portable Wordpress configuration file
This article also strongly recommends using the WP_SITEURL and WP_HOME constants in order to make your Wordpress configuration file more portable, which has the added benefit of making this sort of setup even easier, particularly for those modifying an existing Wordpress site. For background, read my portable Wordpress configuration article but if you’d rather just get to it, add the following two lines to your wp-config.php file, replacing “wordpress” in the first line to whatever directory you want to keep your Wordpress files in.
// ** Hostname settings ** // define('WP_SITEURL', "http://${_SERVER['HTTP_HOST']}/wordpress"); define('WP_HOME', "http://${_SERVER['HTTP_HOST']}");
New Wordpress installations
It’s easy. First, simply unpack Wordpress into a subdirectory of your site. Then all you have to do is copy the index.php file from the WP directory into the root directory of your site, and make a single change. Look for this line:
/** Loads the WordPress Environment and Template */ require('./wp-blog-header.php');
And change it to this, replacing “wordpress” in the path to whatever directory you installed WP into.
/** Loads the WordPress Environment and Template */ require('./wordpress/wp-blog-header.php');
And that’s it! Your blog content will now appear at the root of your site, and your admin area is now at http://www.example.org/wordpress/wp-admin/.
Moving your existing Wordpress installation
Rearranging an existing Wordpress site is a bit trickier than doing this with a clean installation, but don’t let this deter you. It’s still pretty straight-forward. As above, the first thing you should do is modify your wp-config.php file with the code shown above. This will save you the step of changing the URL settings in the WP administration section.
Once you’ve done this, create the folder you would like to store Wordpress in, making sure the name you choose is the same one you specified in the WP_SITEURL line in your config file. Then move (not copy) all your Wordpress files into this new folder. On a typical Wordpress 2.6 installation, these files include:
- Three folders:
wp-admin,wp-content, andwp-includes - Any files beginning with “wp-” and ending with “.php” (eg,
wp-app.php,wp-atom.php, etc) index.phpxmlrpc.php
The license.txt and readme.html files can be moved, deleted, or ignored, as you wish.
Once these files have been moved, copy (not move) that index.php back into the root directory and open it up in a text editor. Look for this line:
/** Loads the WordPress Environment and Template */ require('./wp-blog-header.php');
And change it to this, replacing “wordpress” in the path to whatever folder you just moved your Wordpress files into.
/** Loads the WordPress Environment and Template */ require('./wordpress/wp-blog-header.php');
No changes to your .htaccess file are necessary. Your admin area is now at http://www.example.org/wordpress/wp-admin/ but you should now see your blog appearing at the root of your site, just the way it always has.
Now that Wordpress is cleanly tucked away into it’s own folder, your web site directory is cleaner and more organized, you will now have an easier time updating Wordpress, and the way is paved for doing easier maintenance using some new features of WP 2.6.


Incoming Links
Comments
Following your directions I successfully moved my wordpress files from my root directory to a subdirectory yet kept the blog URL as http://www.56vibes.com.
I have a different situation on another blog. Right now the WP URL and Blog URL are the same: http://www.katsturtz.com/katblog/
This time I want to do the opposite: Keep the wordpress files in the subdirectory but change the blog URL to the root domain URL. I’ve already edited the wp-config.php file per your Portable WordPress configuration instructions.
My question is: What exact steps do I need to do now to complete the change?
Solution is probably obvious to someone more experienced. But I’m a code-changing novice so am hesitant to just try working backward from the WP root to own directory instructions.
Thanks
@Kat: The process is pretty much the same. If you’ve already added the WP_SITEURL and WP_HOME constants to your config file, now copy the index.php file from the WP directory into the site root and update the path in the require() line, as shown in this article.
However, to move in the direction you’re going, you do need to change your .htaccess file. Open it up and find the RewriteRule line. It probably looks something like this (with some directory name where I have “wordpress” here):
Just remove the subdirectory from the path, like this:
That should do it.
Thanks. The instructions are much appreciated!
Can two WordPress installations run under a same domain? One in the root and the other in the /blog/ directory? I have the one installed in the root, and not when I try to install the second one – that is in the /blog/ I get an 404 error from the first (root) one.
Can I install the two somehow?
@Ivan: Check the
.htaccessfile for the main installation. You’ll need to exclude the /blog directory by doing something like this:RewriteCond %{REQUEST_FILENAME} /blog RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]Hi Kenn,
I presume the main you mean hte one in the root? I changed the .htaccess in hte root to
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} /blog RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPressI added the line that you are suggestiong:
RewriteCond %{REQUEST_FILENAME} /blog
mut my second insallation that is in the /blog/ dir, still opens by itself, but any link on the indexp page – generates a 404 from the root installation.
you can see it here:
http://www.apb.ie/blog/
Any other ideas? Or did I do something wrong?
Looks like you got it working, I see working pages on both the root and /blog sites.
@ Kenn – Thanks for a note! I got it sorted. A bit of Google-ing and one learns how to program in the Zeus scripting language in no time! :)
So a hint for the other who get stuck here: Zeus Web Server – leave the .htaccess alone, but write a decent rewrite.script file in the root.
Your comments are welcome
I'll help where I can. If you're really stuck, I'm available for consulting on an hourly or project basis. Get in touch for details.
Comment notes
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">