Virtualhost For Wildcard Subdomain and Static Subdomain
Asked Answered
M

3

86

I have an odd situation where I want to have the URLs app1.example.com, example.com and *.example.com all using a different virtual host. This is what I have (excluding example.com because it just makes it messier).

<VirtualHost *>
  ServerName app1.example.com
  ServerAlias app1.example.com

  DocumentRoot = /var/www/app1
  # Other configuration for this app here

</VirtualHost>

<VirtualHost *>
  ServerName wildcard.example.com
  ServerAlias *.example.com

  DocumentRoot = /var/www/wildcard
  # other configuration for this app here

</VirtualHost>

The problem is that they conflict. Whichever one is listed first wins out. How can I host both a wildcard virtualhost and a specific one?

Note: I'm not just changing DocumentRoot in the config, so using mod_rewrite to change the DocumentRoot variable does not fix it.

Methoxychlor answered 16/4, 2009 at 22:52 Comment(0)
U
178
<VirtualHost *:80>
  DocumentRoot /var/www/app1
  ServerName app1.example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/example
  ServerName example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/wildcard
  ServerName other.example.com
  ServerAlias *.example.com
</VirtualHost>

Should work. The first entry will become the default if you don't get an explicit match. So if you had app.otherexample.com point to it, it would be caught be app1.example.com.

Uriia answered 16/4, 2009 at 23:2 Comment(11)
Just a question, what does NameVirtualHost *:80 do?Eos
This directive enables the name based virtual hosts and will tell apache to listen on all ip's (*) on port 80. Apache 2.2 Docs: httpd.apache.org/docs/2.2/en/vhosts/name-based.htmlWeidner
Remove the NameVirtualHost *:80: AH00548: NameVirtualHost has no effect and will be removed in the next releaseWeide
The wild card one does not work for me, do you know why?Garrison
Hi, just to add another tip I got: if you have separate files for each than make sure file names of virtual hosts is listed apathetically in the same order, such as: 00app, 01example, 02other.Unbearable
NameVirtualHost *:80 is important for apache 2.2, no need for apache 2.4, I don't know why.Billingsgate
It's all in the order!Parnassian
Does ServerAlias support other expressions other than *, like ServerAlias a[0-5]?*.example.comHogwash
Where do I find this file?Sigismond
how to work with ssl certificate? RewriteCond %{SERVER_NAME} =example.com [OR] RewriteCond %{SERVER_NAME} =*.example.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]Moorefield
Can you also use a wildcard for the TLD? E.g.: example.*Guilbert
L
32

Wildcards can only be used in the ServerAlias rather than the ServerName. Something which had me stumped.

For your use case, the following should suffice

<VirtualHost *:80>
    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/%1/
</VirtualHost>
Lesbianism answered 20/2, 2018 at 20:28 Comment(1)
This is a great solution but for future reference does require a2enmod vhost_alias for the vhost_alias module to be turned on.Extrasystole
D
2

This also works for https needed a solution to making project directories this was it. because chrome doesn't like non ssl anymore used free ssl. Notice: My Web Server is Wamp64 on Windows 10 so I wouldn't use this config because of variables unless your using wamp.

<VirtualHost *:443>
ServerAdmin [email protected]
ServerName test.com
ServerAlias *.test.com

SSLEngine On
SSLCertificateFile "conf/key/certificatecom.crt"
SSLCertificateKeyFile "conf/key/privatecom.key"

VirtualDocumentRoot "${INSTALL_DIR}/www/subdomains/%1/"

DocumentRoot "${INSTALL_DIR}/www/subdomains"
<Directory "${INSTALL_DIR}/www/subdomains/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>

Devault answered 21/1, 2019 at 0:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.