Page 1 of 1
How to create Sub Domains using a script
Posted: Wed Jun 21, 2006 3:10 pm
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
Posted: Wed Jun 21, 2006 3:12 pm
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?
Posted: Wed Jun 21, 2006 3:15 pm
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
Posted: Wed Jun 21, 2006 3:16 pm
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.
Posted: Wed Jun 21, 2006 3:28 pm
by Weirdan
alternatively, you could do this with mod_rewrite
Posted: Wed Jun 21, 2006 3:30 pm
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
Posted: Wed Jun 21, 2006 3:31 pm
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.
Posted: Wed Jun 21, 2006 3:34 pm
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.
Posted: Wed Jun 21, 2006 3:36 pm
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.
Posted: Wed Jun 21, 2006 3:37 pm
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
Posted: Mon Dec 18, 2006 10:49 am
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