Multiple domains (including www-"subdomain") on apache?

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"?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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>

Solution 4:

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>