.htaccess and Subdirectories

Well, in interests of getting some content on this rather dead blog, I figured I should post what I just figured out in terms of how to setup a .htaccess file to handle a website that actually setup in a subdirectory under the main webroot directory.

My own personal solution to organize my sitesSuch as this blog and my anime weblog and be able to quickly upgrade to newer Drupal releases is using soft-links, but not everybody has the knowledge to setup and maintain such as system. Or some web hosting system doesn't give you the ability to create a soft-link or give you space outside of the webroot directory to work work with. I was hit with such a situation at work were an .htaccess solution would actually be easier to explain how to update than to teach how to connect via SSH and create softlinks. Those .htaccess experts can probably tell me what I did wrong, but this seemed to work for me in my test setup. The scenario is a webroot with the following file structure:

/
/404.php
/stage
/stage/index.php
/test
/test/index.php
/website
/website/index.php

Basically, /website needs to act as the "root" site, but going into /stage and /test should be allowed. This was to provide a separation of webroots, but here's the .htaccess file that seemed to work with how I needed to work:

# Set the 404 page
ErrorDocument 404 /404.php

# Turn on Rewrite engine and set base
RewriteEngine on
RewriteBase /

# Rewrite root directory to subdirectory
RewriteRule ^$                        website/            [L]

# Check that the request is not an existing file or directory!
RewriteCond %{REQUEST_FILENAME}     !-f
RewriteCond %{REQUEST_FILENAME}     !-d

# Ignore the target directory
RewriteCond %{REQUEST_URI}             !^/website/

# Ignore other directories at root (must manually add another line for each of them!)
RewriteCond %{REQUEST_URI}             !^/stage/
RewriteCond %{REQUEST_URI}             !^/test/

# Do the redirect 
RewriteRule ^(.+)$                     website/$1     [L]

Basically, any access inside /test, /stage, and /website will be left as is, but any other access will be a) checked that it doesn't already exist as a directory or file yet, if not, then website/ will be appended to the begining of the directory behind the scenes. Basically, /index.php would be rewritten to /website/index.php, /thisdirnotexist/thisfilenotexist.html is rewritten to /website/thisdirnotexist/thisfilenotexist.html, but /test/notexisting.html would be left alone.

Hopefully this helps others.