Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am struggling with this all the day, no answer i found here as well. Please advice how to setup proper a subdomain i need.

My Apache config has 2 domains configured (on same IP), for the domain2.com i need to setup a sub-domain. Here is what i have so far, but the subdomain keeps redirecting me to domain2.com (main site).

<VirtualHost 11.11.11.11:80>
ServerName domain1.com
ServerAlias domain1.com *.domain1.com
DocumentRoot "C:/wwwmap/domain1.com"
</VirtualHost>

<VirtualHost 11.11.11.11:80>
ServerName domain2.com
ServerAlias domain2.com *.domain2.com
DocumentRoot "C:/wwwmap/domain2.com"
</VirtualHost>

        <VirtualHost 46.4.24.4:80>
        ServerName projects.domain2.com
        DocumentRoot "C:/wwwmap/projects"
        </VirtualHost>

The DNS entry is: projects in CNAME domain2.com

Trying to remove ServerAlias domain2.com *.domain2.com worked so far, but then domain2.com is redirecting to domain1.com

What am i doing wrong?

share|improve this question

3 Answers 3

You should use something like this:

NameVirtualHost *:80

<VirtualHost *:80>
 ServerName domain1.com
 ServerAlias domain1.com *.domain1.com
 DocumentRoot "C:/wwwmap/domain1.com"
</VirtualHost>

<VirtualHost *:80>
 ServerName domain2.com
 ServerAlias domain2.com *.domain2.com
 DocumentRoot "C:/wwwmap/domain2.com"
</VirtualHost>

Using this kind of setup the Host: header, that your browser sends, will be enough to differentiate the different entries. You can use something other than "*:80", but this is a basic config that absolutely should work.

share|improve this answer
1  
NameVirtualHost directive is deprecated in Apache 2.4. That one does not work –  Grashopper Oct 7 '12 at 21:42
1  
True, but it will work. Using a VirtualHost as above the first one will always win on the same IP. –  Steve Kemp Oct 7 '12 at 21:43
1  
well, moving the domain2 on first level (above domain1), disabling ServerAlias domain2.com *.domain2.com now did the trick.. but still not sure if it is a proper solution.. –  Grashopper Oct 7 '12 at 22:51
    
I agree. You shouldn't do <VirtualHost IP:port> unless you really have to and your box actually has those IPs bound otherwise it's a pain to diagnose issues. –  Alastair McCormack Oct 10 '12 at 18:43
up vote 1 down vote accepted

The projects.domain2.com vhost has to be defined before the vhost with with wildcard catch domain, than it will work.

<VirtualHost 11.11.11.11:80>
ServerName domain1.com
ServerAlias *.domain1.com
DocumentRoot "C:/wwwmap/domain1.com"
</VirtualHost>

<VirtualHost 11.11.11.11:80>
ServerName projects.domain2.com
DocumentRoot "C:/wwwmap/projects"
</VirtualHost>

<VirtualHost 11.11.11.11:80>
ServerName domain2.com
ServerAlias *.domain2.com
DocumentRoot "C:/wwwmap/domain2.com"
</VirtualHost> 
share|improve this answer

If you had a redirect from one domain to the other before, you might still have this direct cached in your browser. Try clearing your cache.

share|improve this answer
    
no Chris, there was never a redirect. Cannot find DNS issues as well. –  Grashopper Oct 7 '12 at 22:12
    
maybe you should post a bit more like real domains (if possible) and real configs. Apache doesn't just redirect. –  Christopher Perrin Oct 7 '12 at 22:18
    
OK, its not a redirect.. i just realized that it is a tracking script which is served via first domain. No issue with redirect.. it was my fault sorry. –  Grashopper Oct 7 '12 at 22:29
    
so is the problem solved then? –  Christopher Perrin Oct 7 '12 at 22:54
    
well, it seems like.. but i am not sure if it is solved that way that the answer will help others. Moving domains in apache config one level up is not a proper solution as (in my case) domains below will have same issue as soon i try to assign subdomains to them in the future –  Grashopper Oct 7 '12 at 22:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.