Wildcard DNS and PHP
I’ve always been wondering how it works, and I’ve finally found out how it does. I will give a few simple examples how it works and how you can use it.
Step one : Make sure your server/domain allows wildcard DNS and that it is enabled on your server/domain.
Step two : Lets create a file called index.php
now before I continue with step 3, it is nice to know that the wildcard subdomain forwards to the index.php on the main domain so all the following domain pont to the same file.
- foo.domain.com
- bar.domain.com
- foobar.domain.com
like said before all these calls will point to domain.com/index.php. We now can use PHP to understand what we typed in,and what we can do with this data.
step three : Now we can enter the code in the index.php that will identify the subdomain we have requested for further processing.
<?php
$serverhost = explode('.',$_SERVER["HTTP_HOST"]);$subdomain = $serverhost[0];
//check if it really is a subdomain
if (isset($serverhost[2]) and ($serverhost[2] != "") ? $sub = $subdomain : $sub = "" );
// now we can use $sub whereever we want.
echo $sub;
?>
Now the variable “$sub” contains the subdomain we have requested, if we directly called the main url the variable will be empty.
i hope this short and simple tutorial was usefull for you!

























July 12th, 2009 at 11:21 pm
Thank you for your help!
November 13th, 2009 at 6:14 am
Thanks for the process
February 7th, 2010 at 9:44 am
I don’t usually reply to posts but I will in this case. WoW
November 13th, 2010 at 10:42 pm
Thanks for the poem. great work!