Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-line configuration for both www and non-www VirtualHost

I have many VirtualHost and it seems that we need to do these two lines each time:

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

Is there a one-line solution that allows to do this?

<VirtualHost *:80>
  ServerName *.example.com   # works for both http://example.com and http://www.example.com

i.e. without having to duplicate example.com?

like image 565
Basj Avatar asked Feb 13 '18 11:02

Basj


1 Answers

No, it is not possible.

ServerName is used to uniquely identify protocol, name and port of a host. It accepts only one parameter and no wildcard.

You can use wildcards in ServerAlias, and also specify multiple names on it:

ServerAlias *.example.com

or

ServerAlias www1.example.com www2.example.com www3 foo

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

like image 174
dr_ Avatar answered Sep 18 '22 14:09

dr_