Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Handling many sub-domains

    Hi,

    I have a site which has many sub-domains. Accessing each of them should lead to a simple page, saying:
    "hello, ".$subdomain
    , where $subdomain is the current sub domain in the address bar.

    Can it be done using only 1 file, or I must put in each and every sub-domain a different HTML file with different text?

    Thanks.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Unless you directed each sub domain to the same document root folder, which would defeat the point. If not then you would need a separate file in each of the sub domains document root.

    I have seen one website that does this. If you didnt bother setting up any virtual hosts for the other domains then you could technically put any subdomain in to the address bar you wanted and it will still direct to the same page.

    For instance if you had a site called http://isanoob.com and you wanted this page to display "{subdomain} is a noob"

    so submitting timmy.isanoob.com would submit timmy "is a noob"

    If this is the kind of thing you are thinking of then you could achieve this with file. In fact you page directs for the sub domoain wouldn't actually be directed anywhere other than the document root of the main domain. All you would have to do is remove the subdomain part of the page query. This can be done easily with $_SERVER['HTTP_HOST'];

    PHP Code:
    <?PHP
    function get_subdomain() {
        
    // Explode the HOST into it's constituent parts:
        
    $parts explode('.'$_SERVER['HTTP_HOST']);
        
    $cp count($parts);

        
    // If a 1 part hostname like 'localhost' don't bother with a subdomain:
        
    if ($cp == 1) { $subdomain ''; }
        
    // Otherwise, generate our subdomain by using the last 2 parts
        
    else { $subdomain ".{$parts[$cp-2]}.{$parts[$cp-1]}"; }

        return 
    $subdomain;
    }


    $subdomain=get_subdomain();
    echo 
    $subdomain.' is a noob';
    ?>
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •