Search code examples
php.htaccesssubdomainwildcard-subdomain

Dynamically assign a subdomain that is not related to a folder


I need to create a sub-domain dynamically. What my code does is load html code from DB then displays it.

There are no sub folders or anything like that. I want a url that looks like this XXXXXX.com/loadfile.php?website=test to look like test.XXXXXX.com.


Solution

  • Something as basic as this should do the trick:

    <?php
    // assuming you have $_SERVER['SERVER_NAME'] reliably set and available
    // i.e. something like $_SERVER['SERVER_NAME'] = 'XXXXXX.com';
    
    $subdomain = $_GET['website'] . '.' . $_SERVER['SERVER_NAME'];
    

    You might want to validate the input etc.