How to create Sub Domains using a script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ibizconsultants
Forum Commoner
Posts: 35
Joined: Tue Sep 07, 2004 12:07 pm

How to create Sub Domains using a script

Post by ibizconsultants »

Hi all,

I have a requirement where in each time a user registers on the system, I need to create and assign a subdomain for him.

The server is not a shared server and is dedicated to the project.

Can anyone guide me on the best way to approach this situation.

Thanks in advance.

iBizConsultants
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

1. What is the procedure for creating a subdomain on your server?
2. How could you accomplish this with a script? What would the script need to do?
3. What if you need to delete a subdomain?
ibizconsultants
Forum Commoner
Posts: 35
Joined: Tue Sep 07, 2004 12:07 pm

Post by ibizconsultants »

Thanks for the reply.
astions wrote:1. What is the procedure for creating a subdomain on your server?
2. How could you accomplish this with a script? What would the script need to do?
3. What if you need to delete a subdomain?
1. I really dont know the procedure to create a subdomain. If you are asking about the business rule, then anyone who signs up gets a subdomain.

2. The script would just create the subdomain on the server.

3. I would not delete the subdomain, as the users are never deleted from the system.

Can anyone help me with how to do this.

Thanks again
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Here is one way... Actually we use it at http://www.aatraders.com for all of the personal blogs at that site. IE: http://panamajack.aatraders.com

First, your DNS definitions for your domain name have to include a *.aatraders.com wildcard A record, so that all subdomains are mapped to your one server's IP address. 'panamajack' is just one subdomain, 'tarnus' is another one. You need the * to catch them all.

Then, in your web server's configurations file httpd.conf, within your virtual host definition for aatraders.com you have to specify a * wildcard alias, so that every possible subdomained host name points to the same document root of aatraders.com.

Code: Select all

<VirtualHost *:80>
        DocumentRoot /***/***/***
        ServerAdmin webmaster@aatraders.com
        ServerName www.aatraders.com
        serveralias profiles.aatraders.com aatraders.com *.aatraders.com
        ErrorLog /***/***/aatradersweb.error_log
        CustomLog /***/***/aatradersweb.access_log combined
        ScriptAlias /cgi-bin/ /***/cgi-bin/
</VirtualHost>
Now you are ready to catch the subdomains within PHP, $subdomain = explode(".",$_SERVER["HTTP_HOST"]); and do whatever you want with it.

Code: Select all

if(strtolower($_SERVER["HTTP_HOST"]) != "www.aatraders.com" && strtolower($_SERVER["HTTP_HOST"]) != "aatraders.com")
{
	$domain = explode(".", $_SERVER["HTTP_HOST"]);
	$subdomain = $domain[0];
}
else
{
	$subdomain = '';
}
Then depending upon what is in the $subdomain variable I include the appropriate php file. If there is a subdomain I load the blog code. If there isn't a subdomain then I load the main page.
Last edited by AKA Panama Jack on Wed Jun 21, 2006 3:29 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

alternatively, you could do this with mod_rewrite
ibizconsultants
Forum Commoner
Posts: 35
Joined: Tue Sep 07, 2004 12:07 pm

Post by ibizconsultants »

Weirdan wrote:alternatively, you could do this with mod_rewrite
Can you please guide me on how to do this with mod_rewrite.... wont all the urls be re-written if i use a general rule. Can you please help me understand in more details... am new to this completely. Sorry for the trouble
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Weirdan wrote:alternatively, you could do this with mod_rewrite
True, but I personally hate using mod_rewrite. Plus some hosting comapanies do not allow you access to that and you can usually get away with using the * as a server alias.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or with mod_vhost_alias: http://httpd.apache.org/docs/2.0/en/vhosts/mass.html
This method is most suited for services like hosting providers... if everything you need is to replace urls like 'site.com/script.php?user=username' with 'username.site.com' then I'd suggest to go either with mod_rewrite route or APJ suggestion.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Plus some hosting comapanies do not allow you access to that and you can usually get away with using the * as a server alias.
The server is not a shared server and is dedicated to the project.
...which usually means you have full control over the server configuration.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Can you please guide me on how to do this with mod_rewrite.... wont all the urls be re-written if i use a general rule. Can you please help me understand in more details... am new to this completely. Sorry for the trouble
http://httpd.apache.org/docs/2.0/en/vho ... le.rewrite
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Weirdan wrote:
Can you please guide me on how to do this with mod_rewrite.... wont all the urls be re-written if i use a general rule. Can you please help me understand in more details... am new to this completely. Sorry for the trouble
http://httpd.apache.org/docs/2.0/en/vho ... le.rewrite
Would I Save It As .htaccess Under htdocs Folder
Post Reply