Solved

Auto subDomain creation..

Posted on 2006-10-30
4
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

705 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