1

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]
GDP
  • 8,109
  • 6
  • 45
  • 82
  • Possible duplicate of [.htaccess rewrite subdomain to directory](http://stackoverflow.com/questions/10642426/htaccess-rewrite-subdomain-to-directory) – Marcos Pérez Gude Nov 03 '15 at 17:02
  • Tried those before posting this, and didnt work, presumably due to the root already being a subdomain, thus the narrower question. – GDP Nov 03 '15 at 17:27
  • Take care of `Options +FollowSymLinks -MultiViews` and adding A entries in your DNS zone it works perfectly. I'm sure because I use it many times – Marcos Pérez Gude Nov 03 '15 at 17:29
  • As I thought this should, but it is not, thus the question with clarifications. : ) – GDP Nov 03 '15 at 17:53

1 Answers1

1

In order to get v2.domain.com working on the server you need to add that to your Apache config pointing to this document root. You can use ServerAlias Then when you type in v2.domain.com it will server the files in the root.

For your rewrite for subdomains you can use this .htaccess rules.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.|v2\.)
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteRule ((?!subdomains/).*) /subdomains/%1/$1 [L]
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • The v2 aspect is/has been working fine...but this generates an Internal Server Error. – GDP Nov 03 '15 at 17:18
  • AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. – GDP Nov 03 '15 at 17:24
  • 1
    `RewriteRule ^((?!subdomains/).*) /subdomains/%1/$1 [L]` @GDP for the last line in rule above. – hjpotter92 Nov 03 '15 at 18:00
  • Just now getting back to this. @hjpotter92 thanks for straightening that out. – Panama Jack Nov 03 '15 at 19:43