0

I have an Apache 2.4 server with two enabled virtual hosts:

/etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

/etc/apache2/sites-enabled/my-website.conf

<VirtualHost my-website.com:80>
    ServerAdmin myself@my-website.com
    ServerName my-website.com
    Redirect permanent "/" "https://my-website.com/"
</VirtualHost>

<IfModule mod_ssl.c>
    <VirtualHost my-website.com:443>
    ....
    </VirtualHost>
</IfModule>

I created a domain name pointing to the same machine: my-other-website.com.

When I issue an http request (browser, curl,...) on my-other-website.com, I would expect it to serve the default virtual host. Instead, I get 301 redirected to https://my-website.com/.

How come?

Changing <VirtualHost *:80> into <VirtualHost _default_:80> didn't help.

1

VirtualHost marching is a bit peculiar and particular.

The “problem” is that you mix a *:80 with hostname:80 and the latter is the preferred match when a request for that ip-address & port number combination is received.

And *:80 is only the default VirtualHost for requests that don’t have the same ip-address as your my-website.com domain.

See https://httpd.apache.org/docs/2.4/vhosts/details.html#hostmatching

When the connection is first received on some address and port, the server looks for all the VirtualHost definitions that have the same IP address and port.

If there are no exact matches for the address and port, then wildcard (*) matches are considered.

If no matches are found, the request is served by the main server.

If there are VirtualHost definitions for the IP address, the next step is to decide if we have to deal with an IP-based or a name-based vhost.

| improve this answer | |
  • Thanks. I think I get it. So what's the point of the hostname in <VirtualHost hostname:80> if only ServerName/ServerAlias is used to match the request? – Jérôme Feb 25 '19 at 23:57
  • It is intended to bind virtual hosts to specific ip-address/port combinations and that allows Apache httpd both ip-based VirtualHosting (which is not that common a requirement anymore this day and age but was pretty useful with SSL before SNI , for HTP 1.0 clients) and allows different default virtualhosts for each ip-address on a server – HBruijn Feb 26 '19 at 6:20
2

Make both virtual hosts as

<VirtualHost *:80>

However i would use an IP for both hosts, it can be same IP <VirtualHost xx.xx.xx.xx:80>

| improve this answer | |
  • That would indeed be a solution – HBruijn Feb 25 '19 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.