Add wildcard subdomains to localhost with Apache

I am trying to replicate the setup I have a remote server on my local server/machine, that is, having wildcard subdomains with the following virtual hosts in place.

<VirtualHost *:80>
    DocumentRoot "/Users/User/Sites/tbi"
    ServerName tbi.loc
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/User/Sites/tbi"
    ServerName subdomains.tbi.loc
    ServerAlias *.tbi.loc
</VirtualHost>

This setup doesn't seem to work. I do have access to http://tbi.loc, but I cannot connect to any subdomains. What I aim to accomplish is having the www subdomain for non-registered visitors and a separate subdomain for each registered user.

The document root for both virtual hosts is the same (I have tried using different document roots to no avail) since I determine what needs to happen based on the subdomain.

Please, note that I would like to accomplish this without having to rely on .htaccess (if at all possible).

The Apache hosts file contains two entries for this virtual host.

127.0.0.1 *.tbi.loc
127.0.0.1 tbi.loc

Solution 1:

  1. Do you have NameVirtualHost *:80 configured in you apache config? If not, it would give the tbi.loc site when connecting to www.tbi.loc.

  2. Do you have dns/hostfile entries for www.tbi.loc, subdomain.dbi.loc, *.tbi.loc pointing to your test server? If not, your browser wouldn't know where to connect to.