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.

after messing around a while I decided to ask here:

I have a vhost and want to use 2 domains on this server. My apache configuration file looks something like this:

NameVirtualHost *
<VirtualHost *>
    ServerName www.domain1.de	
    DocumentRoot /var/www/folder1/

</VirtualHost>

<VirtualHost *>
    ServerName www.domain2.de
    DocumentRoot /var/www/folder2/
</VirtualHost>

On the configuration page for the domains of my vhost both domains assigned to the server ip.

The problem now is:

  • www.domain1.de works
  • domain1.de works
  • www.domain2.de works
  • domain2.de does not work

Has any one any idee why the second domain only works with the added "www"?

share|improve this question
    
how's your dns configured - does it resolve both domain2.de and www.domain2.de to the same ip ? –  pQd Jun 9 '09 at 14:48

4 Answers 4

up vote 12 down vote accepted

domain1.de works because www.domain1.de is the first VirtualHost and is served as default. You need to add ServerAlias domain2.de to www.domain2.de for the shorter version to work as well (you should add ServerAlias for www.domain1.de too).

If you don't want www.domain1.de to be served as default add another VirtualHost at the begining serving some simple HTML file.

share|improve this answer
    
1000 x tnx! as usual a very simple solution. –  TheHippo Jun 9 '09 at 14:56

If you want to canonicalize your domains (e.g. redirect all domain.de to www.domain.de) you can use something like mod_rewrite:

<VirtualHost *>
  ServerName www.domain.de
  ServerAlias domain.de

  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www
  RewriteRule /(.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>

The three Rewrite lines will basically:

  1. Check to see if the URL starts with 'www'
  2. If not, redirect to http://www.domain.de/[whatever]

If you go to 'domain.de' it will see that it doesn't start with 'www' and redirect to 'www.domain.de'.

If you want to apply this to every site you host, you can do like this:

<VirtualHost *>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www
  RewriteRule /(.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>

<VirtualHost *>
  ServerName www.domain1.de
  DocumentRoot /wherever/1
</VirtualHost>

<VirtualHost *>
  ServerName www.domain2.de
  DocumentRoot /wherever/2
</VirtualHost>

Then when someone tries to go to 'domain1.de' it will hit the first virtualhost, which will redirect them automatically to 'www.[whatever domain they typed in]'.

This way, if you go to 'domain1.de' it will see that it doesn't start with 'www' and redirect to 'www.domain1.de'. For 'domain2.de', it will redirect to 'www.domain2.de'. Every time you add a new site, you will automatically get this feature, which can be really handy if you're adding/maintaining a lot of websites.

share|improve this answer

www.domain.com and domain.com can theoretically be entirely different websites. You must specify both. You could use a ServerAlias in your existing VirtualHost blocks, or, you could do this to do a 301 Redirect for SEO.

NameVirtualHost *
<VirtualHost *>
    ServerName domain1.de  
    DocumentRoot /var/www/folder1/
</VirtualHost>

<VirtualHost *>
    ServerName www.domain1.de  
    Redirect permanent / http://domain1.de/ 
</VirtualHost>

<VirtualHost *>
    ServerName domain2.de  
    DocumentRoot /var/www/folder2/
</VirtualHost>

<VirtualHost *>
    ServerName www.domain2.de  
    Redirect permanent / http://domain2.de/ 
</VirtualHost>
share|improve this answer
    
This is a messy and complicated solution that doesn't make a whole lot of sense. You could use something like a mod_rewrite snippet to check for 'www' at the start and redirect if not. Otherwise your apache config balloons and it becomes easy to miss things or typo. –  Dan Udey Jun 10 '09 at 0:51
    
Rewrite rules would be three extra lines (engine on, condition, and rule) per domain, this is four. I fail to see how it's particularly messy or complicated in comparison, especially if you do what most people do and have each domain's vhosts in its own .conf file. –  ceejayoz Jun 10 '09 at 1:57
    
see @Dan Udey's answer –  hayalci Jun 14 '09 at 21:02

Use ServerAlias to associate more than 1 domain to a virtual host

NameVirtualHost *
<VirtualHost *>
    ServerName www.domain1.de 
    ServerAlias domain1.de *.domain1.de  
    DocumentRoot /var/www/folder1/
</VirtualHost>

<VirtualHost *>
    ServerName www.domain2.de
    ServerAlias domain2.de *.domain2.de  
    DocumentRoot /var/www/folder2/
</VirtualHost>
share|improve this answer

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.