Auto subDomain creation..

i need to create subdomains dynamically via PHP in an Apache based web server.

im leaning towards XYZ.COM/NEWDOMAIN versus NEWDOMAIN.XYZ.COM

as simple as possible, maybe just the building blocks.

editing the httpd.conf and named.db are required? or can't i just create a subdirectory for the New User off the main, and call it good?



Who is Participating?
sebastienboConnect With a Mentor Commented:

I always do the following:

1) forward subdomain or * wildcard to your webserver with A records
2) change your very first page (index.php) by putting a subdomain handler in it

subdomain handler is just removing the "" from the $_SERVER['HTTP_HOST'];  
the only thing left is your subdomain now you have two solutions

1) you: include($subdomain.'/'.index.php) <- with this one you have
2) or you: header("Location:$subdomain/index.php") <- with this method you will fall on this url:

I hope this helps
CraigHarrisConnect With a Mentor Commented:
Setup * in your DNS (as applicable for your domain)

In httpd.conf (or vhosts.conf if using Apache 2.2.x), add:

    ServerAlias *

To your virtual host definition (or server-wide if not using vhosts)

Then use mod_rewrite so that Apache will always pick correct directory - no special PHP handling needed -- it will just work :)

So enable the mod_rewrite module, then below the server alias, add:

  RewriteEngine On
  RewriteCond   %{HTTP_HOST}                 ^.+\.example\.com$
  RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
  RewriteRule   ^(.+)\.example\.com(.*) /path/$1/$2

Then you just create a folder for each subdomain (including www)

So you have something like:


Where /path is the applicable document root.

Apache will always make sure the correct files get used -- just make sure you create folders as required.

This is a clean solution with minimal developer/admin overhead ... using PHP things can get very very messy and are unlikely to appear correctly to end users.

If you need further explanation just ask.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.