Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Location
    Connecticut
    Posts
    5,441

    Script: Subdomain->Domain Redirect

    PHP Code:
    <?php
        $domain 
    $_SERVER['SERVER_NAME'];
        
    $data $_SERVER['REQUEST_URI'];
        
        if(
    fnmatch('*.*.*',$domain)) {
            
    $domain explode('.',$domain);
            
    $newDomain $domain[1].'.'.$domain[2].'/'.$domain[0].$data;
            
            
    header('Location: '.$newDomain);
        } else {
            echo 
    'Could not forward from this URL.';
        }
    ?>
    This script takes a subdomain such as 'subdomain.example.com/index.php?a=test' and transforms it into 'example.com/subdomain/index.php?a=test'

    This is helpful is you want to have subdomains point to a folder on your main domain.

    Note: This does not support TLDs such as '.co.uk', '.com.cn', or any TLDs with two parts.

  2. #2
    Join Date
    Nov 2005
    Posts
    268
    To support those TLDs simply adding a $domain[3] and another .* inside the fnmatch should work.
    PHP Code:
    <?php 
    $domain 
    $_SERVER['SERVER_NAME']; 
    $data $_SERVER['REQUEST_URI']; 
     
    if(
    fnmatch('*.*.*.*',$domain)) { 
    $domain explode('.',$domain); 
    $newDomain $domain[1].'.'.$domain[2]'.'.$domain[3].'/'.$domain[0].$data
     
    header('Location: '.$newDomain); 
    } else { 
    echo 
    'Could not forward from this URL.'

    ?>
    Last edited by Korvan; 12-05-2005 at 02:45 PM.

  3. #3
    Join Date
    Feb 2003
    Location
    Connecticut
    Posts
    5,441
    As far as I know, that would only match .*.* extensions, not .*

  4. #4
    Join Date
    Nov 2005
    Posts
    268
    Quote Originally Posted by DanX
    As far as I know, that would only match .*.* extensions, not .*
    Yes, that is correct, it will only support .co.uk and not .com for example.

    Also considering peoples main subdomain is usually www you can code it like this for support of both types of TLD's

    PHP Code:
    $domain $_SERVER['SERVER_NAME']; 
    $data $_SERVER['REQUEST_URI']; 
     
     
    $domain explode('.',$domain); 
    if(
    $domain[0] != 'www')
    {
    $newDomain 'http://www.'.$domain[1].'.'.$domain[2].($domain[3] ? '.'.$domain[3] : '').'/'.$domain[0].$data
    header('Location: '.$newDomain); 
    echo(
    "You are being redirected to <A HREF=\"{$newDomain}\">{$newDomain}</A>");
    exit();
    }
     
    //continue page output 
    This will only work if you use www to access the root of your site.

  5. #5
    Join Date
    Feb 2003
    Location
    Connecticut
    Posts
    5,441
    Well the script was originally coded since someone wanted to redirect the same way cPanel does.

    The echo is sort of pointless since nothing is output after the header.

  6. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Related Posts from theWHIR.com

Posting Permissions

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