1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Dynamic subdomains through .htaccess

Discussion in 'Programming' started by qwikad.com, Jul 9, 2014.

  1. #1
    Currently the states and cities are shown this way:

    example.com/-5-California
    or
    example.com/294-Dayton-Springfield
    or
    example.com/22-Fort-Smith
    or
    example.com/-35-New-York

    etc...

    I want to get them changed via .htaccess to:

    california.example.com
    or
    daytonspringfield.example.com
    or
    fortsmith.example.com
    or
    newyork.example.com

    etc...

    The numbers for states and cities:

    States: $row['countryid']
    Cities: $row['cityid']

    The names for states and cities:

    States: $row['countryname']
    Cities: $row['cityname']

    How would you create dynamic subdomains using this information?

    Thanks!
     
    qwikad.com, Jul 9, 2014 IP
  2. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #2
    As you will have to customize your code anyway, there is no need of .htaccess for that, an easy way is simply to add a DNS record for *, then using PHP you can take the "hostname" used, follow my example:

    
    
    $host_name = explode('.',$_SERVER['SERVER_NAME']);
    if ( (count($host_name) == 3) AND (strtolower($host_name[0]!=="www")) ) {
    //We Got a Subdomain
    $value=strip_tags($host_name[0]); //Let's always clean the input...
    $query = mysql_query("SELECT countries.countryid as state,cities.cityid as city FROM countries,cities WHERE (cities.name LIKE '$value' OR countries.name LIKE '$value')");
    $data = mysql_fetch_array($query);
    echo "<pre>".print_r($data,true)."</pre>";
    //If value is equal to a city we will get an array like this:
    //Array ( city => 44, state => 0)
    //If value is equal to a state we will get an array like this:
    //Array ( city => 0, state => 32)
    //If we get no matches then we can simply show the homepage again:
    //Array ( city => 0, state => 0) --> include "homepage.php";
    //Other Code here...
    
    } else {
    //We Got a Regular Domain
    //Let's show the homepage...
    }
    
    
    Code (markup):
    Mind this code is raw, can be optimised and can be really powerful to do what you need.
     
    YoGem, Jul 14, 2014 IP
    jimmyt200388 likes this.
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    Thank you for your input. It looks like it may work but I am not sure. Will test it later. Is it something you used in the past and it worked?
     
    qwikad.com, Jul 15, 2014 IP