rendered paste bodyRewriteEngine on
RewriteLog /usr/local/apache/logs/rewrite.log
RewriteLogLevel 9
RewriteMap lc int:tolower
#if www.example.com end and go to www.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.+) - [L]
#if www.foo.example.com 301 to foo.example.com - THIS ONE'S IMPORTANT
#I honestly don't know what the 2nd or 3rd lines of this section do
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.example\.com$ [NC]
RewriteRule ^(.+) %{HTTP_HOST}=$1 [C]
RewriteRule ^(.*)=(.*) ${lc:$1}$2 [C]
RewriteRule ^www\.([^.]+)\.example\.com http://$1.example.com/ [R=301,L]
#these last 3 lines are to rewrite foo.example.com/something to www/example.com/foo/something
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/icons
RewriteRule (.*) /www/example.com/%1$1 [C]
RewriteCond %{REQUEST_URI} ^.*[^/]$
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule (.*) $1/ [L]