Virtualhost For Wildcard Subdomain and Static Subdomain

121,856

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>

Share:
121,856

Related videos on Youtube

Dave
Author by

Dave

Danger Never Takes a Vacation.

Updated on January 05, 2021

Comments

  • Dave
    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 (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.

  • Pedro Moreira
    Pedro Moreira almost 10 years
    Just a question, what does NameVirtualHost *:80 do?
  • Andreas Hinderberger
    Andreas Hinderberger over 9 years
    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.html
  • nerdoc
    nerdoc about 9 years
    Remove the NameVirtualHost *:80: AH00548: NameVirtualHost has no effect and will be removed in the next release
  • ericn
    ericn about 9 years
    The wild card one does not work for me, do you know why?
  • Ugur DOKMECI
    Ugur DOKMECI over 8 years
    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.
  • Leo Lee
    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_
    AO_ about 7 years
    It's all in the order!
  • endo64
    endo64 over 6 years
    Does ServerAlias support other expressions other than *, like ServerAlias a[0-5]?*.example.com
  • Maciek Semik
    Maciek Semik over 5 years
    Where do I find this file?
  • vishal-mote
    vishal-mote almost 4 years
    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]
  • GTodorov
    GTodorov about 3 years
    Can you also use a wildcard for the TLD? E.g.: example.*