didn't know how to put this into to a title. Ok my idea is to have any subdomain working on my site, and for example if I type:


(my site has a unique folder for every subdomain, and within that folder, there is just a simple config file that sees the 'what' ( first part of subdomain) and loads database information based on the string and the whole site is populated. So if I put lol.example.com I have the same site, but with different information just based on database. Do you understand?

And then if people type the main site example.com or www.example.com they have the main site which is completely different from the subdomains.

So how can I do this?

I have this idea for a site of mine, in which the main site example.com will be a information site with a payment system. And when users pay me, I add their info to to an unique file where all other user info is located ( database info ), and then their site is whatever subdomain they have chosen .example.com , and there they have their info and can login.

edit: I just can't figure out how to allow any subdomain to be inserted in the url, loading the sub site, as grabbing the first part of the subdomain is easy and then everything would work.

  • You have a different folder for each subdomain? Apart from not making use of programming scripts (see NiloVelez's answer) you are creating a nightmare for yourself.
    – Steve
    Jul 14 '12 at 21:48

Generally, you're describing wildcard DNS. But you'll have to talk to your host about implementing it, as they may or may not even support it, or only in particular cases(cf. Dreamhost).


I assume two posible scenarios:

In the first, each site has its own configuration file, named after the subdomain:

$host == $_SERVER['HTTP_HOST'];
$parts = explode('.',$host);
$subdomain = $parts[0];

if (file_exists('./site_config_'.$subdomain.'.php')){

This way, if you enter site1.mysite.com, site_config_site1.php will be searched for, if it doesn't exist, site_config_default.php

The second alternative implies a little more of work. You have to create a set of tables with a different prefix for each site:

$host == $_SERVER['HTTP_HOST'];
$parts = explode('.',$host);
$db_prefix = $parts[0];
// you should check here that the prefix actually exists

This way you will have:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.