I am trying to set up an apache 2.4 server with massive subdomains using ServerAlias. The basic configuration for the main domain works just fine (main domain => 200, all subdomains => 404):
UseCanonicalName Off
<VirtualHost example.com>
ServerName example.com
ServerAlias www.example.com
VirtualDocumentRoot "/var/www/vhosts/example.com/httpdocs"
DirectoryIndex index.php index.html
<Directory />
Options +Indexes +FollowSymLinks +Includes
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
AllowOverride All
</Directory>
</VirtualHost>
But when I add the lines for my generic vhost set up the web server returns an 404 status code on every vhost (subdomain) request:
UseCanonicalName Off
<VirtualHost example.com>
ServerName example.com
ServerAlias www.example.com
VirtualDocumentRoot "/var/www/vhosts/example.com/httpdocs"
DirectoryIndex index.php index.html
ServerAlias *.example.com
VirtualDocumentRoot "/var/www/vhosts/example.com/%1.0"
DirectoryIndex index.php index.html
<Directory />
Options +Indexes +FollowSymLinks +Includes
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
AllowOverride All
</Directory>
</VirtualHost>
I think it must have to do something with the right folder path. The folder path always looks like this:
/var/www/vhosts/maindomain.com/subdomain.maindomain.com
What I need is simply a configuration that matches the main domain and generic sub domains associated to that. So,
www.example.com
example.com
matches folder path:
/var/www/vhosts/example.com/httpdocs
And
ci.example.com
tracker.example.com
other.example.com
should match to its folder path like this:
/var/www/vhosts/example.com/ci.example.com
/var/www/vhosts/example.com/tracker.example.com
/var/www/vhosts/example.com/other.example.com
Also, for some strange reason, if I change it to %0.0 in the subdomain path, the subdomains works, but the main domain does not. So, with using that, it's the other way around. Main domain => 404, all subdomains => 200.
I found the explanation for the magic string literals for folder names in the apache 2.4 docs: http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html But rather than solving my issue, it confuses me more and more...
The web server runs under openSUSE 42.2 minimal. As you may already suggested, the real domain name is changed here to example.com. Any help to this issue would be great.
Edit: As I said also down below in my comment to the first answer, I need a solution, that does not necessarily require any RewriteRule in the vhost config. Because I am using php systems, that do have their own (sometimes real complex) RewriteRules, so I do not want to get in trouble with that .htaccess stuff. But any other options, except cgi is possible. And yes, symlink support is enabled (or possible to enable it).
Edit:
The symlink example helped me to get the basic idea. A bit rethinking brought me to this solution below. First found rule always applies, so it worked for me:
UseCanonicalName Off
<VirtualHost *:80>
DocumentRoot "/var/www/vhosts/example.com/httpdocs"
ServerName www.example.com
ServerAlias example.com
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/vhosts/example.com/httpdocs"
ServerName example.com
ServerAlias *.example.com
VirtualDocumentRoot "/var/www/vhosts/example.com/%1"
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
Allow from all
</Directory>
</VirtualHost>
apachectl -t and apachectl -S says that this is ok. Tested and worked! :-)