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

I'm trying to setup two domains correctly. I have some issues I hope you can help me. Site one's conf:

<VirtualHost myipaddress:80>
    ServerName www.domain1.com
    ServerAdmin hostmaster@domain1.com
    DocumentRoot /home/domain1/public_html
</VirtualHost>

My other domain conf is:

<VirtualHost myipaddress:80>
    ServerName www.domain2.com
    ServerAlias *.domain2.com domain2.com
    ServerAdmin hostmaster@domain2.com
    DocumentRoot /home/domain2/public_html
</VirtualHost>

The default site is disabled. The problem is that when accessing "domain2.com" from my browser, it always redirects to "www.domain1.com". It only works when I excplicitly access "www.domain2.com". I have also other domains like "domain1.net", "domain1.info" pointing to my server but at this moment are not configured either setup on Apache yet I can access from browser and always accessing to "www.domain1.com".

By the way is there any possible configuration over Apache to handle IP only, I mean if I type "http://myipaddress/" I get the "www.domain1.com"... Arrgh.

share|improve this question
    
What kind of redirect? Apache won't send you to another host without being configured to do so; that config may be elsewhere (.htaccess file?) or dynamic content running within Apache may be doing it (are you running PHP or other dynamic content?). –  Shane Madden Mar 30 '12 at 22:18
    
The way you can avoid the IP address defaulting to the default site is to make another blank default site and have a vhost pointing to it. To make that site the default on Debian-based is to have the symlink in /etc/apache2/sites-available/ start with the lowest number, like 000. –  paradroid Mar 30 '12 at 22:29
add comment

2 Answers

In addition to Chris his answer. If you want to redirect every subdomain site1.domain2.com, site2.domain.com. You can easily add a wildcard serveralias:

<VirtualHost *:80>
ServerAlias domain2.com *.domain2.com
DocumentRoot /var/www/domain2
</VirtualHost>

Now about handling the IP. You can define a domain to point to an IP. This is what Apache also gets in the header of the http request. The problem is that when you you visit an IP, there is no information to the webserver what you actually want. So this means that you will need to provide an IP/website if you want to implement this and change your vhosts accordingly! (also make sure that your domains point to their respective IP's).

<VirtualHost 1.2.3.4:80>
ServerAlias domain2.com *.domain2.com
DocumentRoot /var/www/domain2
</VirtualHost>

<VirtualHost 5.6.7.8:80>
ServerAlias domain1.com *.domain1.com
DocumentRoot /var/www/domain2
</VirtualHost>
share|improve this answer
add comment

You should add NameVirtualHost *:80 to your central configuration and use <VirtualHost *:80> instead of the <VirtualHost IP:80>. Lastly ensure you got your DNS work correctly.

Update: To get domain2.com point to www.domain2.com you can either use a ServerAlias

ServerAlias domain2.com

a rewrite rule like this (there are a couple of versions floating around the internet)

 RewriteEngine On
  RewriteCond %{HTTP_HOST} ^example\.com
  RewriteRule (.*) http://www.example.com/$1 [R=301,L]

or a separate <VirtualHost>. I'd go with ServerAlias domain2.com.

To see what is served first by Apache, you can type apache2ctl -S on the shell. First served defaults to the IP address.

share|improve this answer
1  
No, you do not need to do this, and you should not do this if you have Apache listening on more than one IP address and you want to keep them separate. –  paradroid Mar 30 '12 at 22:24
    
Can you point me to the line in topic, where this comment is any relevant to the question? –  Chris Mar 31 '12 at 7:08
    
@paradroid is saying that there is no need to change to *:80, it has no bearing on the problem. –  SimonJGreen Mar 31 '12 at 14:04
add comment

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.