Apache config stuff: # For branches RewriteCond %{HTTP_HOST} ^(.+)\.(.+)\.(.+)\.(.+)\.inside\.eseoh\.com$ RewriteRule (.*) /var/www/%4.inside.eseoh.com/%3/%1/%2/$1 # For trunks RewriteCond %{HTTP_HOST} ^(.+)\.(.+)\.(.+)\.inside\.eseoh\.com$ RewriteRule (.*) /var/www/%3.inside.eseoh.com/%2/%1/$1 Goal: When a URL is: http://at16.intranet.astoria-branches.vmnexus.inside.eseoh.com/ it should match the first rule When a URL: is http://intranet.astoria_turkey-trunk.vmnexus.inside.eseoh.com/ it should match the second rule.