Solved

Auto subDomain creation..

Posted on 2006-10-30
4
427 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to remove  "0":  in a json string 4 44
How can I split a variable 19 45
Select Query Fails in PHP but not in TERMINAL Mysql 9 29
php time 12 23
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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