2010/08/12

Installing WordPress in a Subdirectory

If others have posted about this already, I couldn't find it last night.

We're converting a domain for a client from hosting some demo content to hosting a WordPress site. In the process, we need to keep the previous URLs valid, but let WP provide the document-root. I also like to keep things organized, so WP will live in its own directory (/WP).

First, extract WordPress to the /WP directory, and update its configuration per the manual. Your life will be easier if you STOP before step 6 (install.php), and setup these rewrite rules first...

For this situation we have two .htaccess files.

One at the document root...
# /var/www/default/.htaccess
RewriteEngine On
RewriteBase /

# by default, /wp-admin redirects to the full
# path to /wp-admin/ - in this case /WP/wp-admin/
# which is BAD, and breaks login.
# this rewrite preempts that by adding the
# trailing slash, so that wp-admin goes directly
# to login without the path
RewriteRule ^(.*)wp-admin$ $1wp-admin/ [R=302,L]

# Shortcut for directory index
RewriteRule ^$ /WP/ [PT,L]
RewriteRule ^/$ /WP/ [PT,L]

# Pass all non-file queries to WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /WP/$1 [PT]


And one for WordPress...
# /var/www/default/WP/.htaccess
# managed by WP, essentially its default
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
OK, you can now proceed with step 6 on the manual - install, without using /WP in the URL.

(2010-08-13: update for directory index shortcuts)