如何让Apache在有无www的情况下提供服务

我目前有这个

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

如果我要求使用“ WWW”,该方法就可以正常工作

我尝试在没有WWW的情况下添加

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

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

但Apache无法启动,表示配置不正确

May 08 22:19:14 host-1 systemd[1]: Starting The Apache HTTP Server...
May 08 22:19:15 host-1 apachectl[27876]: AH00526: Syntax error on line 21 of /etc/apache2/sites-enabled/EXAMPLE.com.conf:
May 08 22:19:15 host-1 apachectl[27876]: Invalid ServerName "*" use ServerAlias to set multiple server names.

official config doc is here http://httpd.apache.org/docs/2.0/vhosts/examples.html#default its just so late for me to read it now - I will tomorrow and will reply ... but I hope this can helps someone out there configuring their Apache

I have tried changing it to this as per this guide to no avail https://support.elastx.se/hc/en-us/articles/214239326-Catch-all-virtualhost-with-301-redirect-in-apache

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

<VirtualHost *:80>
    ServerName www.EXAMPLE.com
    ServerAlias *
    Redirect 301 / http://EXAMPLE.com/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>