Virtualhost For Wildcard Subdomain and Static Subdomain
Solution 1
<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.
Solution 2
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>
Solution 3
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>
Related videos on Youtube
Comments
-
Dave over 3 years
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 (excludingexample.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 usingmod_rewrite
to change the DocumentRoot variable does not fix it. -
Pedro Moreira almost 10 yearsJust a question, what does
NameVirtualHost *:80
do? -
Andreas Hinderberger over 9 yearsThis 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.html
-
nerdoc about 9 yearsRemove the
NameVirtualHost *:80
: AH00548: NameVirtualHost has no effect and will be removed in the next release -
ericn about 9 yearsThe wild card one does not work for me, do you know why?
-
Ugur DOKMECI over 8 yearsHi, 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
. -
Leo Lee about 8 years
NameVirtualHost *:80
is important for apache 2.2, no need for apache 2.4, I don't know why. -
AO_ about 7 yearsIt's all in the order!
-
endo64 over 6 yearsDoes
ServerAlias
support other expressions other than*
, likeServerAlias a[0-5]?*.example.com
-
Maciek Semik over 5 yearsWhere do I find this file?
-
vishal-mote almost 4 yearshow 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]
-
GTodorov about 3 yearsCan you also use a wildcard for the TLD? E.g.: example.*