VirtualHosts for SubDomains with WildCard

Alles, was den Apache betrifft, kann hier besprochen werden.

VirtualHosts for SubDomains with WildCard

Postby yzjmuqcf » 02. May 2015 11:59

I have multiple VirtualHosts defined for a domain, let's call it example.com.
My first VirtualHost had the ServerName "example.com" and the ServerAlias "www.example.com".
My second VirtualHost had the ServerName "sub1.example.com".

After changing the first VirtualHost from the ServerAlias "www.example.com" to " *.example.com "
the DocumentRoot of the first VirtualHost is always loaded ignoring which SubDomain is called.
I need the wildcard definition in the Alias for my SSL cert...
E.g. I type sub1.example.com into the address bar of my browser and instead of loading the contents
from "/var/www/sub1.example.com" I always get the content from "/var/www/example.com".
How can I solve this issue to use a wildcard in the ServerAlias and use additional specified subdomains?

This is my first VirtualHost:

Code: Select all
<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        Require all granted
    </Directory>
</VirtualHost>

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

    <Directory /var/www/example.com>
        Require all granted
    </Directory>

    SSLEngine on
    SSLCertificateFile /etc/ssl/custom-ca.crt
    SSLCertificateKeyFile /etc/ssl/custom-ca.key
</VirtualHost>


This is my second VirtualHost:

Code: Select all
<VirtualHost *:80>
    ServerName sub1.example.com
    RedirectPermanent / https://sub1.example.com/
</VirtualHost>

<VirtualHost _default_:443>
    ServerName sub1.example.com
    DocumentRoot /var/www/sub1.example.com

    <Directory /var/www/sub1.example.com>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    SSLEngine on
    SSLCertificateFile /etc/ssl/custom-ca.crt
    SSLCertificateKeyFile /etc/ssl/custom-ca.key
</VirtualHost>
yzjmuqcf
 
Posts: 3
Joined: 02. May 2015 11:54
Operating System: Linux Ubuntu 12.04

Re: VirtualHosts for SubDomains with WildCard

Postby Nobbie » 02. May 2015 12:47

Change the order of the VirtualHost Definitions, put sub1.example.com at the beginning.
Nobbie
 
Posts: 13183
Joined: 09. March 2008 13:04

Re: VirtualHosts for SubDomains with WildCard

Postby yzjmuqcf » 02. May 2015 14:54

Nobbie wrote:Change the order of the VirtualHost Definitions, put sub1.example.com at the beginning.


I cannot because this is not one file but two config files "example.com.conf" and "sub1.example.com.conf" in /apache2/sites-available/
which are added by the command "a2ensite example.com.conf" and "a2ensite sub1.example.com.conf".
I do not think I can manipulate the sorting order here... Any other idea or example for ordering with the two different files?
yzjmuqcf
 
Posts: 3
Joined: 02. May 2015 11:54
Operating System: Linux Ubuntu 12.04

Re: VirtualHosts for SubDomains with WildCard

Postby Nobbie » 02. May 2015 15:53

There is no other solution than changing the order. If a2ensite does not offer proper options, simply dont use it (its only a tool). Simply edit the configuration files and provide the information you need. Create your own virtualhosts file and insert the VirtualHosts there.
Nobbie
 
Posts: 13183
Joined: 09. March 2008 13:04

Re: VirtualHosts for SubDomains with WildCard

Postby yzjmuqcf » 03. May 2015 11:59

I merged the two config files as suggested into one config file and moved the virtual hosts in an order like:

sub1.example.com
...
*.example.com

Now everything works fine. Thank you very much!!! :D
yzjmuqcf
 
Posts: 3
Joined: 02. May 2015 11:54
Operating System: Linux Ubuntu 12.04


Return to Apache

Who is online

Users browsing this forum: No registered users and 34 guests