Coders Corner / Psuedo Sub-Domains With PHP
What do YOU think about this Script? Add YOUR comments at the end!
Here’s a handy bit of PHP code you can use to simulate Third Level Domains with minimal involvement from your ISP or webmaster.What do YOU think about this Script? Add YOUR comments at the end!
Here’s a handy bit of PHP code you can use to simulate Third Level Domains with minimal involvement from your ISP or webmaster. For those of you unfamiliar with them, Third Level Domains are those whose names replace the commonly used “www.” For example, my main Traffic Filter is at http://www.adultsitelisting.com but I commonly refer to it as http://AdultSiteListing.com without the “www.” It’s the “www.” That I want to replace, rather than eliminate – more for marketing and aesthetics than for “function.”
As I’m about to start working on a new project site, “Asian Adult Site Listing” I asked “Buran” (our intrepid CTO) to setup http://asian.adultsitelisting.com for me – as well as a few other sub-domains while he was at it. He offered me a better solution however, one that was more flexible for the uses I had in mind, and one that would allow me to add, subtract, rename, or redirect these psuedo sub-domains at will.
He renamed my “home page” to “default.html” (it could be any name you wish) and setup a new home page at index.php with the code given below as the only content. Now, all I had to do was add the desired sub-directories (folders) to my site, for example:
http://www.adultsitelisting.com/asian
http://www.adultsitelisting.com/amateur
http://www.adultsitelisting.com/hardcore
Because the DNS settings were not changed (other than the addition of the *.domain wild card and “ServerAlias *.sitename.com” in the Apache config – have your webmaster or ISP do this for you), any browser request for asian.adultsitelisting.com would go to the new index page where the PHP code would look down the path for a folder called “asian” (http://www.adultsitelisting.com/asian) and redirect the surfer to that directory (just as if it was a “true” Third Level Domain). If that directory is not found, the surfer would be directed to the default page, “default.html.” Cool, huh? Try it on your site!
$defaultpage = “default.html”;
$sitename = “adultsitelisting.com”;
if(eregi(“^” . $sitename . “$”, $HTTP_HOST)) {
include($defaultpage);
} else {
$subdomain = explode(“.”, $HTTP_HOST);
$subdomain = strtolower($subdomain[0]);
if($subdomain == “www”) {
include($defaultpage);
} else {
if(file_exists($DOCUMENT_ROOT . “/” . $subdomain)) {
Header(“Location: http://www.$sitename/$subdomain”);
} else {
include($defaultpage);
}
}
}
Now, it’s very important for you to add the standard PHP “containers” to this script. It must start with < ? and end with ? > (I only omitted them from the script here because they kept it from being displayed in the text box).
[ MORE SCRIPTS | CODER’S CORNER >>
Reader Comments on this Article:
Comment by:Summary:
Stephena handy script indeed
michael
All I get is a 403 error