virtualhost – Apache virtual hosts not working as expected – my website is redirected to the default virtual host


I’m configuring a new server (droplet) with Ubuntu 18.04.1 LTS. I installed apache and configured virtual hosts. The server name is www.speedysoftware.com and this is the value of /etc/hostname. The problem is, I’m trying to redirect http://www.speedysoftware.com/ to a specific virtual host, but it’s redirected to the default virtual host. On the other hand, http://www.speedysoft.com/ and http://www.speedy-software.com/ are redirected to the correct virtual host, which should be the same as http://www.speedysoftware.com/

File /etc/apache2/sites-available/000-default.conf:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerAdmin webmaster@speedy.net
    DocumentRoot /var/www/general

    <Directory /var/www/general>
        Options +FollowSymLinks
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

File /etc/apache2/sites-available/speedysoftware.conf:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.speedysoftware.com
    ServerAlias speedysoftware.com *.speedysoftware.com
    ServerAlias speedy-software.com *.speedy-software.com
    ServerAlias speedysoft.com *.speedysoft.com
    # ... (more ServerAliases)

    ServerAdmin webmaster@speedy.net
    DocumentRoot /var/www/speedysoftware

    <Directory /var/www/speedysoftware>
        Options +FollowSymLinks
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

You can see that http://www.speedy-soft.com/ is redirected to the default virtual host, and this is correct. What is the problem with the configuration?

After I made changes, I reloaded the settings by sudo systemctl restart apache2.

Regarding redirecting, in this case I don’t want to redirect. I want them to be considered separate websites although they lead to the same virtual host. In other cases I do redirect, for example I redirect http://www.speedypedia.org/ to http://www.speedypedia.info/, and I redirect http://ww.speedysoftware.com/ (or any other subdomain, including http://speedysoftware.com/) to http://www.speedysoftware.com/.

Update – I checked and http://ww.speedysoftware.com/, http://wwww.speedysoftware.com/ and http://speedysoftware.com/ all lead to the correct virtual host. Only http://www.speedysoftware.com/ doesn’t lead to the correct virtual host.