ServerAlias not working

.htaccessapache-2.2serveraliasvirtualhost

I have a VPS, that I have configured to host multiple websites with name based hosting. It is all good while only using example.com, and www.example.com. It also works with example.net, but when I try example.net, it reverts to my default site configuration, which just shows my default (empty) index.html page.

Here's the default file:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Here's a configuration for the example.com site:

    <VirtualHost *:80>
     ServerAdmin admin@example.com
     ServerName example.com
     ServerAlias www.example.com
     DocumentRoot /srv/www/example.com/public_html/
     ErrorLog /srv/www/example.com/logs/error.log
     CustomLog /srv/www/example.com/logs/access.log combined
<Directory /srv/www/example.com/public_html/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

And here is the config for the example.net site:

<VirtualHost *:80>
     ServerAdmin admin@example.net
     ServerName example.net
     ServerAlias www.example.net
     DocumentRoot /srv/www/example.net/public_html/
     ErrorLog /srv/www/example.net/logs/error.log
     CustomLog /srv/www/example.net/logs/access.log combined
<Directory /srv/www/example.net/public_html/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

Where could the problem be?

I believe, that there is something going wrong with the ServerAlias property.

Could it be because of the way the site's are built? Because example.com is a Joomla site, and example.net is a Zend Framework site. Just in case, I'll also insert the .htaccess files for example.net, since example.com has it's disabled:
example.net:

SetEnv APPLICATION_ENV development

RewriteRule  ^(browse|config).*  - [L]
ErrorDocument 500 /error-docs/500.shtml
SetEnv CACHE_OFFSET 2678400


<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Fri, 25 Sep 2037 19:30:32 GMT"
Header unset ETag
FileETag None
</FilesMatch>

RewriteEngine On
RewriteRule ^(adm|statistics) - [L]
RewriteRule ^/public/(.*)$ http://example.net/$1 [R]
RewriteRule ^(.*)$ public/$1 [L]

Any help would be greatly appreciated!

Edit So that my question is ABSOLUTELY clear:
The problem is, that one site works with both www prefix as well as without it, and the second one does not. I would like to know how to enable the second site to work with www prefix as well.

Best Answer

Make sure that the order of precedence in configuration directives matches how Apache should handle the request - i.e. you won't have much luck if your directives load like this:

  • example.com
  • default/catch-all
  • universal.lv

Doesn't matter whether they are declared in a single file or multiple files - so long as "universal.lv" is last, it will not be reached because the default/catch-all vhost will precede it.

I usually name my default/catch-all config "z-default.conf" (or, if you have a domain that starts with a "z", "zz" ... etc) for this reason, to ensure that it is processed only if no other vhost matches.

Try renaming accordingly.


Updated: So as not to confuse w/the _default_ VirtualHost directive

Using wildcard DNS to direct all requests to example.com to the server's IP (catches mis-keyed subdomains), Apache parses the following configuration files:

  • 000-default.conf - VirtualHost _default_:80 / ServerName <hostname> - catch requests against the server's IP (can redirect to primary domain or simply block with the default Directory specified in conf.d/security)
  • example.conf - VirtualHost *:80 / ServerName www.example.com
  • example2.conf - VirtualHost *:80 / ServerName www.example2.com
  • zzz-example.conf - VirtualHost *:80 / ServerName example.com / ServerAlias *.example.com - redirects to canonical www domain
  • zzz-example2.conf - VirtualHost *:80 / ServerName example2.com / ServerAlias *.example2.com - redirects to canonical www domain

As documented, Apache selects the first matching VirtualHost (i.e. _default_:80) but this behavior is not desirable if you are using a wildcard in a ServerAlias directive.

Related Topic