Catch all VirtualHost not working

I’m trying to setup a VirtualHost that catches all sub domains that don’t many any other VirtualHosts. But with this, I keep getting the index.html and it doesn’t take me to the error.php page. My OS is Ubuntu 16.04 and I haven’t changed the apache.conf in /etc/apache2. My other VirtualHost is for Nextcloud running at storage.declanmarks.pw.

<VirtualHost *:80>
        DocumentRoot /var/www/main
        ServerName null
        ServerAlias *

        Redirect 404 /error.php
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I not an expert in Apache.

Answer

For a catch-all i.e. default host you don’t need to specify ServerAlias *. Just make sure that this is the first <VirtualHost *:80>.

If the VirtualHost is included from sites-enabled/, you could rename it to begin with prefix 000- or similar, so that it’s the first file in alphabetical order. If no other ServerName or ServerAlias matches, Apache will fall back to the first one.

Attribution
Source : Link , Question Author : Declan Marks , Answer Author : Esa Jokinen

Leave a Comment