We're migrating and consolidating our main site and a number of smaller sites to a new server over the coming months. The new site is being developed at v2.domain.com, using a CNAME entry in the DNS. So...the new directory structure we're working with is the root, with an increasing number of subdomains as shown below.
What I'm trying to accomplish is an .htaccess file that will point to the correct folder, based on the subdomain and show that in the browser,
i.e http://sub.domain.com
will point to root/subdomains/sub
, and show http://sub.domain.com
in the browser, not the root and its folders. Additionally, it needs to recognize that the v2 subdomain is actually the root on this server acting as a temporary www (currently being handled by the CNAME entry) so use the files in the root folder, and when we cut over, behave the same way for www.
root/
index.php
subdomains/
tools/
sales/
testing/
Current htaccess that is NOT working as I'd like.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. Ignore www. OR v2. ??
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteRule (.*) /subdomains/$1 [L]
Update: Working .htaccess, with accepted answer PLUS comment from @hjpotter92 that finished the solution:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.|v2\.)
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule ^((?!subdomains/).*) /subdomains/%1/$1 [L]