<Directory /home/will/sites/>
  Order allow,deny
  Allow from all
  Options +All
  AllowOverride All
</Directory>

# I want a mod_rewrite config that is equivalent to a mod_vhost:
#  VirtualDocumentRoot /var/www/html/%-2.0.%-1/%-3/%-4/
# Except if the tertiary or quaternary parts are missing I want
#  them omitted rather than putting in the '_'. Also I want for
#  the www to be ignored, so www.himinbi.org and himinbi.org
#  are the same files.
# For example:
#  himinbi.org -> /html/himinbi.org/
#  www.himinbi.org -> /html/himinbi.org/
#  odin.himinbi.org -> /html/himinbi.org/odin/
#  covers.odin.himinbi.org -> /html/himinbi.org/odin/covers/

RewriteEngine on
RewriteMap lowercase int:tolower

#RewriteLog logs/rewrite.log
#RewriteLogLevel 5

# this is only to be used for my hosts, so limit to them

RewriteCond %{SERVER_NAME} !himinbi\.org$ [NC]
RewriteCond %{SERVER_NAME} !pcvs\.org$ [NC]
RewriteCond %{SERVER_NAME} !holcomb\.info$ [NC]

RewriteRule . - [S=4]

# mod_rewrite only operates on the file part of the uri, so
#  this concatenates the host name onto the start of the URI
# this will be looped over again, so an environment variable
#  is checked to allow it to be skipped subsequently

RewriteCond %{ENV:NAME_APPENDED} !TRUE

# drop the www at the front if it exists

RewriteCond %{SERVER_NAME} (www.)*(.*) [NC]

# 1.
#
# append the stripped filename to the existing uri and set
#  the environment variable

RewriteRule ^(.+)$ ${lowercase:%2}$1 [E=NAME_APPENDED:TRUE]

# 2.
#
# check if there are more than two parts to the name and if
#  there are move the first one to the beginning of the
#  request and loop

#            [   1    ] [ 2 - n-2 ][ n-1  ][  n ]  [file] 
RewriteRule ^([^/.]+)\.(([^/.]+\.)*[^/.]+\.[^/.]+)/(.*)$ $2/$1/$4 [N]

# 3.
#
# put the search in the proper directory and remove the
#  environment variable for the next run

RewriteRule ^(.*)$ /home/will/sites/$1 [E=NAME_APPENDED:]

# at this point check if the resource exists

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

# This makes it impossible to write redirects for moved files,
#  so only do it if it is a known alias

RewriteCond %{REQUEST_URI} ^/icons [OR]
RewriteCond %{REQUEST_URI} ^/pipermail [OR]
RewriteCond %{REQUEST_URI} ^/doc [OR]
RewriteCond %{REQUEST_URI} ^/error [OR]
RewriteCond %{REQUEST_URI} ^/~ [OR]
RewriteCond %{REQUEST_URI} ^/cgi-bin

# 4.
#
# if it doesn't exist and is a known alias, pass-through

RewriteRule . %{REQUEST_URI} [PT]
