Results 1 to 1 of 1

Thread: Multiple Sub-domain Site Script

  1. #1
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,238
    Thanks
    171
    Thanked 303 Times in 230 Posts

    Multiple Sub-domain Site Script

    Hi folks, my first post here

    I am developing a PHP script that customizes a web page template according to the sub-domain that was used to access the site. Each sub-domain will be owned by a member of the main host site so their details are used to customize the page.

    So my plan is to use .htaccess on LAMP (Linux, Apache etc.) hosting to rewrite the URLs from:

    anysubdomain.mysite.com to www.mysite.com/index.php?sub=anysubdomain
    Then index.php will produce the custom page.

    My understanding is that the DNS Zone File for the website needs to have an entry to handle wildcard subdomains.

    So I can use WHM (Web Host Manager) to add an entry like:

    *.mysite.com. IN A 90.168.0.1

    where the IP and domain name is as per my site.

    Or I ask my Tech support at my host to do this for me.

    So if this works, any sub-domain at my site should display the home page.

    Now, to pass the sub-domain value to the index.php script, I will use .htaccess

    I think the code I need is something like:
    Code:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^[-a-z0-9]{2,}\..+$ [NC]
    RewriteRule ^/(.*)$ /index.php/?sub=%1 [R=301,L]
    This then, allows the sub-domain value to be accessed by the PHP script.

    Well I will be trying to get this working on Monday but thought it would be good to post it up here.

    Note that I have not tested this out yet, it's just what I think may work.

    Update:
    It turned out to be quite simple:

    In cPanel simply add a new subdomain using the wildcard character *

    And here is a test script to use in index.php that displays the name of the subdomain:
    Code:
    <?php
    $host = getenv('HTTP_HOST');
    $pattern = '/([-a-zA-Z0-9]{2,})\.[^c]/';
    preg_match($pattern, $host, $matches);
    $subdomain = $matches['1'];
    
    echo"Subdomain: $subdomain";
    ?>
    Last edited by Andy101; 24 August, 2009 at 03:35 AM.

  2. Thanked by:

    A12Alex (28 August, 2009), Will.Spencer (27 August, 2009)

Similar Threads

  1. Replies: 0
    Last Post: 29 September, 2010, 08:34 AM
  2. Domain for a Script Site
    By shivachettri in forum Domaining
    Replies: 6
    Last Post: 1 April, 2010, 16:08 PM
  3. [WTS] Coupons Site, Great Keyword Domain, Awesome Script
    By ChaoscripT in forum Sites
    Replies: 6
    Last Post: 17 March, 2010, 20:43 PM
  4. Replies: 9
    Last Post: 15 May, 2009, 19:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

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