Forum Moderators: coopster

Message Too Old, No Replies

Create Subdomain with PHP

         

Majid

10:06 am on Jun 6, 2005 (gmt 0)

10+ Year Member



Hello,

First of all, I'm very thankful for your great site .

I have a question.

I would like to create a virtual subdomain with PHP (I think I should do this with mod_rewrite and .htaccess)

for example :
[img54.img.mysite.com...] > [img.mysite.com...]
(the 54 is a variable and img.mysite.com is really a subdomain)

Now , what I should do?

P.S: sorry for my English :(

jatar_k

4:43 pm on Jun 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Majid,

I am not thinking that is quite possible, variable subdomains, though I am not sure.

I believe if it was possible that it would be an Apache question more so than a php one.

You could try our Apache Forum [webmasterworld.com]

Majid

11:33 am on Jun 7, 2005 (gmt 0)

10+ Year Member



Thank you jatar_k,

Ok I will asked this question in Apache forum.

Thanks in advanced.

Romeo

12:59 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Hi Majid,

you can't "create" subdomains with PHP, but you could use them in PHP if your server setup lets you do so.

First, your DNS definitions for your domain name have to include a *.img.example.com. wildcard A record, so that all subdomains are mapped to your one server's IP address.
'Img54' is just one subdomain, 'img55' 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 img.example.com you have to specify a * wildcard alias, so that every possible subdomained host name points to the same document root of img.example.com.

Now you are ready to catch the subdomains within PHP
$subdomain = explode(".",$HTTP_HOST);
and do whatever you want with it, perhaps redirecting like the following simple example:

if(is_dir("$YOUR_DOCUMENT_ROOT/$subdomain[0]")) {
header("Location:http://img.example.com/$subdomain[0]");
} else {
header("Location:http://img.example.com/$somewhereelse");
}

Regards,
R.