0

I would like to have subdomains for my web app like so:

  • company1.mysite.com
  • company2.mysite.com

How do I go about doing this? Do I need a virtual host set up per company?

Or can I have a catchall like:

  • *.mysite.com

What's the typical approach here?


My thoughts on how to show the correct content is that I will take the prefix part of the URL, check the database for the id of that company, and then just use the id to bring back the correct content...

The problem is that i'm unsure of how to create subdomains programmatically in PHP and Apache.

I am using Windows (so locally I will be editing the windows host file)

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233

1 Answers1

1

First You've to define wildcard rule in your dns server.

For example in bind (linux and etc.) I do like this:

somedomain.com.      IN      A       111.222.222.222
*.somedomain.    IN      A       111.222.222.222

and then in virtualhost file add this:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName somedomain.com
  ServerAlias *.somedomain.com
  DocumentRoot /var/www/public
</VirtualHost>

this example for one big single application that get subdomain and works with it's logic.

so if every app is separate then:

<VirtualHost *:80>
    ServerName somedomain.com
    ServerAlias *.somedomain.com
    VirtualDocumentRoot /var/www/%1/public
</VirtualHost>
num8er
  • 18,604
  • 3
  • 43
  • 57