0

I am trying to setup two virtual hosts on my server such that all subdomains should go to Dir1 and one specific subdomain should go to Dir2.

Basically, I want to use these two

  1. *.domain.com > Dir1
  2. special.domain.com > Dir2

I have following two virtual hosts entries in my conf files

<VirtualHost *:80>

    ServerName special.domain.com
    ServerAlias special.domain.com
    DocumentRoot /var/www/html/Dir2
</VirtualHost>

<VirtualHost *:80>

    ServerName www.domain.com
    ServerAlias *.domain.com
    DocumentRoot /var/www/html/Dir1
</VirtualHost>

Now the problem is that wildcard domain just works fine...but special.domain.com also goes to Dir1 instead of Dir2.

Any idea what am I missing here?

2 Answers2

1

This is due to the order of the directives, once Apache reads the combined configuration file.

Apache will use the first matching site ServerName or ServerAlias it finds, so if your wildcard host appears first in the config files then apache will match using that and never see the special config (because special.domain.com also matches *.domain.com). So - you need to make sure the special.domain.com conf file is loaded and parsed first so that apache will choose it as the first match. Only sites that don't make that first match will then "fall through" and match *.domain.com instead.

On Ubuntu, Debian, or other Debian-based systems, these files are found in /etc/apache2/sites-enabled (these are in fact symbolic links to the actual .conf files in /etc/apache2/sites-available). Apache will read the files in that directory in alphabetical order, so you can force a file to read first by renaming its link to "001-something.conf" rather than "something.conf". Debian uses this trick by default - it sets up the default website as "001-default.conf" (linked to ../sites-available/default.conf). This causes the default virtualhost to load first so it's the one you see if nothing later matches. Just like you can name a conf file 001-something.conf you could also name it: ZZ-something.conf to make it load last. In all these cases you are renaming the files in /etc/apache2/sites-enabled (not /etc/apache2/sites-available).

Under Redhat / Centos / Fedora, you combine vhosts into a single configuration file. If that's your environment then just re-order them until they work, following instructions below.

To fix your problem experiment with renaming your conf files in /etc/apache2/sites-available so that your wildcard site loads last (Rename it to start with ZZ-). Or, if you determine it's loading last already, try making it load first (rename it to start with 001 or 002- or something). One of them should fix it and it will behave as you expect.

0

At this much information I would say this is all about debugging and digging.

From https://stackoverflow.com/a/758383/10477798 I can tell this indeed will work, checked.

ServerAlias is not needed and not correct in your special.domain.com part, so let's try deleting it.

<VirtualHost *:80>
    ServerName special.domain.com
    DocumentRoot /var/www/html/Dir2
</VirtualHost>

<VirtualHost *:80>
    ServerName www.domain.com
    ServerAlias *.domain.com
    DocumentRoot /var/www/html/Dir1
</VirtualHost>

Someone said that "names of virtual hosts is listed apathetically in the same order", s to w, checked.

For more, you might want to specify:

  1. Your Apache version
  2. Are you directly accessing special.domain.com or by any redirect
  3. As always, is there anything in the access log and error log
  4. Where do you put this configuration, are they all together or separate

And go on...