Solved

Auto subDomain creation..

Posted on 2006-10-30
4
422 Views
Last Modified: 2008-03-10


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?


advice?

-n8

0
Comment
Question by:n8dog
4 Comments
 
LVL 5

Accepted Solution

by:
sebastienbo earned 250 total points
ID: 17835539
Hi,

I always do the following:

1) forward subdomain or *.yourdomain.com 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 ".yourdomain.com" 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 http://subdomain.yourdomain.com/
2) or you: header("Location:$subdomain/index.php") <- with this method you will fall on this url: http://yourdomain.com/subdomain

I hope this helps
0
 
LVL 7

Assisted Solution

by:CraigHarris
CraigHarris earned 250 total points
ID: 17838206
Setup *.example.com in your DNS (as applicable for your domain)

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

    ServerAlias *.example.com

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:

/path/www/index.php
/path/xyz/index.php
/path/subdomain2/index.php

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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now