www和非www VirtualHost的单行配置

Bas*_*asj 6 apache apache2 virtualhost apache2.4

我有很多VirtualHost,似乎我们每次都需要做这两行:

<VirtualHost *:80>
  ...
  ServerName www.example.com
  ServerAlias example.com
Run Code Online (Sandbox Code Playgroud)

是否有允许这样做的单行解决方案?

<VirtualHost *:80>
  ServerName *.example.com   # works for both http://example.com and http://www.example.com
Run Code Online (Sandbox Code Playgroud)

即无需复制example.com

dr0*_*r01 4

不,这是不可能的。

ServerName用于唯一标识主机的协议、名称和端口。它只接受一个参数并且不接受通配符。

您可以在 中使用通配符ServerAlias,也可以在上面指定多个名称:

ServerAlias *.example.com
Run Code Online (Sandbox Code Playgroud)

或者

ServerAlias www1.example.com www2.example.com www3 foo
Run Code Online (Sandbox Code Playgroud)

来源:https ://httpd.apache.org/docs/2.4/mod/core.html#serveralias