I have trouble understanding how _default_
vhost works in Apache.
I understand the _default_
should be first, not 100% sure about that eider, but documentation says
If the request contained an unknown or no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).
Does that^ refer to first address/port but NOT also wildcard address and port ?
Because …
With this below config, wildcard port on _default_:*
and port *:80
hardcoded in the defined site …
When accessing the server IP (http://123.123.123.123
) it matches the second virtualhost. I was expecting to match the first one (_default_
). Since the ip address is not speciffically defined in any Vhost / ServerName
<VirtualHost _default_:*>
DocumentRoot /var/www/html/default
</VirtualHost>
<VirtualHost *:80>
ServerName mysite.tld
ServerAlias www.mysite.tld
DocumentRoot /var/www/html/mysite.tld
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.tld [NC]
RewriteRule ^(.*)$ https://www.mysite.tld%{REQUEST_URI} [R=301,L]
</VirtualHost>
With this other config, if I hardcode port 80 in both vhosts, it properly matches the first one when accessing server by ip address http://123.123.123.123
and second vhost when accessing by name http://mysite.tld
<VirtualHost _default_:80>
DocumentRoot /var/www/html/default
</VirtualHost>
<VirtualHost *:80>
ServerName mysite.tld
ServerAlias www.mysite.tld
DocumentRoot /var/www/html/mysite.tld
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.tld [NC]
RewriteRule ^(.*)$ https://www.mysite.tld%{REQUEST_URI} [R=301,L]
</VirtualHost>
So first config is incorrect ? I cannot use <VirtualHost _default_:*>
to match http://123.123.123.123
?
If I defined them both as wildcard port:
<VirtualHost _default_:*>
and second one:
<VirtualHost *:*>
Then it also properly matches ip on the default one and my sites on the second one.