how can we create subdomain through php dynamically

sweta

Registered
Jul 1, 2009
1
0
51
hello friends,

i want to create subdomain through php i have read many forum and i found that i have to place some code in htaccess file but i unable to find htaccess file in cpanel plz help me.
 

eth00

Well-Known Member
PartnerNOC
Mar 30, 2003
721
1
168
NC
cPanel Access Level
Root Administrator
You would need an IP based site to do this.

The htaccess can be created within public_html/.htaccess , simply place the file there and it should work.
 

Spiral

BANNED
Jun 24, 2005
2,018
8
193
You would need an IP based site to do this.

The htaccess can be created within public_html/.htaccess , simply place the file there and it should work.
On the contrary, the account DOES NOT need to have it's own dedicated IP (IP based account) but the virtualhost and DNS do need to be setup to operate with a wildcard. Setting up fancy re-writes won't be of much use if Apache does not know to accept the site handling for the subdomain in the first place!

Something like this in /usr/local/apache/conf/httpd.conf:
Code:
    ServerName [url=http://forums.cpanel.net]mydomain.com[/url]
    ServerAlias [url=http://forums.cpanel.net]www.mydomain.com  *.mydomain.com[/url]
And in your DNS zone file in /var/named/mydomain.com.db:
Code:
* IN A 1.1.1.1
(Obviously mydomain.com would be replaced with your own domain and 1.1.1.1 would be replaced by the IP being used on the account irregardless whether it is using the server shared IP or it's own IP)

Don't forget that you would want to restart both Apache and DNS servers after making these updates and you will also need to run the Cpanel Apache distiller script so that your changes are not automatically deleted later.

As for .htaccess, you can use rewrites to detect what HTTP_HOST has been used to connect and parse off different content depending on the URL used to connect or in the reverse rewrite folder addresses to neater looking subdomains. You can also do this through script as well in the index but neither will work unless you have wildcard DNS and wildcard host setup for the account.
 
Last edited:

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,212
15
313
Houston, TX
cPanel Access Level
Root Administrator
Another solution I've seen is to create a wildcard subdomain in cPanel that points to a folder, and in that folder have an index.php that controls what to display when a specific subdomain is requested. However, depending on what you're doing, this may or may not be a good solution for what you have in mind.
 

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,212
15
313
Houston, TX
cPanel Access Level
Root Administrator
hello friends,

i want to create subdomain through php i have read many forum and i found that i have to place some code in htaccess file but i unable to find htaccess file in cpanel plz help me.
Yet another solution is to use our APIs. You could build a PHP app that calls the cPanel APIs via your cPanel account and use those to create/remove subdomains as needed. I'll be glad to introduce you to how to use our APIs and assist you with using our APIs.