I need to use Apache to achieve such a config, and I'd like to have some advice.

I have a domain: example.com. We have deployed a website under /var/www/html accessible via www.example.com. In addition, we allow users to sign up and access contents (these users then become our tenants). Such tenant-specific contents are deployed under /var/www/tenants. For each of the tenants, we create a subdomain for them. For example, for tenant A, we create tenanta.example.com. Each time one navigates to that url, he can access some content specific to tenant A.

Now tenant A wants to set up a vanity domain for tenanta.example.com. Say the vanity domain is: help.tenanta.com, where tenanta.com is a domain registered by tenant A. He sets up a CNAME record to point help.tenanta.com to tenanta.example.com.

Now it comes to the Apache part. How do I set up the config file so that a request for help.tenanta.com fetches the content under /var/www/tenants, instead of /var/www/html?

Here is what I tried:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName www.example.com
    ServerAlias example.com, cdn.example.com
    # See: http://stackoverflow.com/questions/2297403/http-host-vs-server-name
    UseCanonicalName on

    DocumentRoot /var/www/html

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerAlias *.example.com
    UseCanonicalName on

    DocumentRoot /var/www/tenants

But in such a setting, a request to help.tenanta.com still goes to /var/www/html, instead of /var/www/tenants.

Note: similar questions have been asked on serverfault, like: Apache virtual host based on CNAME

,but they don't address the wildcard subdomain issue.


First off, I'll also note that the use CNAME records is not a factor, name-based virtual host functionality is based on the HTTP Host header (ie, the host part of the request URL) alone.

If the idea is that you mean to have this working without adjusting the Apache httpd configuration when adding customer-specific vanity domains, I see no way that you can have this work based on name-based virtual hosts (ie, based on ServerName / ServerAlias) directly.

What could work is relying on the default vhost behavior when there is no match for ServerName / ServerAlias:

The default name-based vhost for an IP and port combination

If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.

Of course, if you want to serve this over HTTPS (which is increasingly considered mandatory for any type of service), that will require adding certificates usable for all these customer domains anyway.
(Consider automation rather than one static configuration that will work for everything.)

  • Thanks for the explanation, Håkan Lindqvist. It make sense to me. – lgc_ustc Jan 20 '18 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.