How to redirect all wildcard subdomain to a particular subdomain? Having issues with a subdomain which runs nextcloud server

apache-2.4nextcloudwildcard-subdomain

I want wildcard subdomains that do not exist like x.example.com, y.example.com etc., to redirect to 404.example.com.

I am using Apache 2.4. How can I do this?

What I tried:

My 404.conf:

<VirtualHost *:80>
    ServerName 404.example.com
    ServerAlias *.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/web/404"

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

<VirtualHost *:443>
    ServerName 404.example.com
    ServerAlias *.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot "/var/web/404"

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

    SSLCertificateFile /fullchain.pem
    SSLCertificateKeyFile /privkey.pem
</VirtualHost>

<Directory "/var/web/404">
    Options FollowSymlinks ExecCGI
    AllowOverride None
    Require all granted
</Directory>

This actually worked. 123l4k.example.com gets redirected to 404.example.com. But the nextcloud subdomain, nc.example.com, also redirects to 404.example.com. Other subdomains are working fine.

I have to disable the virutalhost 404.conf to be able to access the nextcloud subdomain.

My nc.conf:

Alias /nextcloud /var/web/nextcloud/

<VirtualHost *:80>
  ServerName nc.example.com
  ServerAlias nextcloud.example.com
  DocumentRoot "/var/web/nextcloud/"

  RedirectPermanent / https://nc.example.com

  ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
  CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined

#RewriteLog "/var/log/apache2/nextcloud_rewrite.log"
</VirtualHost>

<VirtualHost *:443>
  ServerName nc.example.com
  ServerAlias nextcloud.example.com
  DocumentRoot "/var/web/nextcloud/"

  ErrorLog ${APACHE_LOG_DIR}/nextcloud_error.log
  CustomLog ${APACHE_LOG_DIR}/nextcloud_access.log combined

  SSLCertificateFile /fullchain.pem
  SSLCertificateKeyFile /privkey.pem
</VirtualHost>


<Directory "/var/web/nextcloud/">
  Options +FollowSymlinks +ExecCGI
  AllowOverride None
  Require all granted

 <IfModule mod_dav.c>
  Dav off
 </IfModule>

 SetEnv HOME "/var/web/nextcloud"
 SetEnv HTTP_HOME "/var/web/nextcloud"
</Directory>

<Directory "/var/web/nextcloud/data/">
  Require local
</Directory>

How can I keep the behaviour of wildcard domains redirecting to 404.example.com and be able to access my nextcloud server on nc.example.com?

Best Answer

The Apache webserver reads the vhost config files in alphabetically order. This implies that the 404.conf files is read before the nc.conf file. Thus the host is first matched with the ServerAlias *.example.com directive, this matches and the other vhosts are ignored. By renaming the nc.conf file to 00_nc.conf the Nextcloud configuration will be loaded first and thus matched against nextcloud.example.com, and other domain is matched against 404.conf which is loaded later.