Installing WordPress in a subdirectory, running it from the root directory

I may be completely alone in this, but I've never been a fan of installing anything straight in to the root directory on a web server. I've always opted for installing various bits of software in to their own subdirectories.

This has served me particularly well if I've needed to install several different bits of software on the same website, for example a piece of forum software to run alongside an existing CMS. Everything stays neat and tidy in its own subdirectory.

It also works really well for developing something new alongside an existing live enviroment. You can work peacefully inside the subdirectory, and even use .htaccess to restrict access to protect the innocent. When you're ready to go live, you can redirect traffic from your root directory to the subdirectory.

So I finally decided to publish my new website, and went about redirecting my root directory to the subdirectory where I had installed WordPress. I have my own method for doing this, but I decided to see what the official WordPress documentation says.

It details two main methods. The first method describes copying the index.php & .htaccess file to the root directory, tweaking the source code of the index.php file, and then changing the Site Address (URL) in Settings > General to remove the subdirectory. The subdirectory then becomes transparent to visitors, who would be able to browse the website on URLs such as http://marktyrrell.com/about.

The second method describes using a simple .htaccess file in your root directory, to redirect visitors to the subdirectory. Using this method, visitors would see the subdirectory in all URLs, you're simply redirecting anyone that lands on the root URL straight in to that subdirectory.

I opted to stick with my own method which I have been using for a long time, and works with various pieces of web software. To use this method with WordPress you first change your Site Address (URL) in Settings > General, removing the subdirectory to show something like http://marktyrrell.com. Ignore the error message you get after clicking save. You now create the following .htaccess file in your root directory (obviously modifying it to your domain and directory).

.htaccess
# Turn on rewrites.
RewriteEngine on

# Don't apply to URLs that go to existing files or folders.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Only apply to URLs that aren't already under /wp.
RewriteCond %{REQUEST_URI} !^/wp/

# Rewrite all those to insert /wp.
RewriteRule ^(.*)$ /wp/$1

# Redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?marktyrrell.com$
RewriteRule ^(/)?$ wp/ [L]

Importantly, clear your web browser cache and delete any cookies for the domain. After you've done this navigate to your website root directory, and you should see your WordPress installation sans the subdirectory in the URL.

I prefer this method as I wasn't too keen on copying and modifying the index.php file. If anyone knows of any potential drawbacks to my method, or why going down the modified index.php route is better, drop me a line in the comments.

Archives

2012
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
2011
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
2010
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec