Subdomains on the fly - but w/out creating a sub directory?

Discussion in 'PHP' started by rob_v, Oct 31, 2008.

  1. #1
    Ok - Im looking for something a bit off kilter here.

    I have a site right now that mimics tinyurl.
    basically you put in a long URL and and it returns you a small URL.
    right now the format is
    mydomain.com/smallurl

    I want to add functionality so I could this :
    smallurl.mydomain.com

    What I think I have to do is mod my https.conf file to allow *. subdomains.
    then add a mod rewrite rule to hit my php code to lookup the "smallurl"
    Does that sound right?

    Any other ideas?
     
    rob_v, Oct 31, 2008 IP
  2. six.sigma

    six.sigma Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes.
    That's is basically.

    You first need wildcards subdomains, that *.yourdomain.com points to some PHP file (or maybe just the index of your site), and then trough htaccess redirect to the proper directory,website or whatever.

    Also, you could just use PHP to analyze the requested URL (subdomain) and redirect the user to the site, if that's located in the database.

    As for example:
    
    <?PHP
    $data=explode(".mydomain.com",strtolower($_SERVER['SERVER_NAME']));
    $data=$data[0];
    $data=str_replace("www.","",$data);
    if($data){
    include("../connect.php");
    
    $result=mysql_query("SELECT * FROM links WHERE id LIKE '%$data%'",$conn);
    while($link = mysql_fetch_array($result)){
    if(strtolower($link[id])==$data){
    header("location: $link[website]");
    break;
    exit;
    }
    }
    }
    echo"<meta http-equiv=\"refresh\" content=\"2;URL=http://www.mydomain.com/\">The code <b>$data</b> doesnt represent a valid tiny url.<br><br>Please wait while you are being redirected to the main site.";
    ?>
    
    PHP:
     
    six.sigma, Oct 31, 2008 IP
  3. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #3
    yeah six.sigma is on the right way.

    but it goes tighter.

    setup wildcard "*" subdomains to point at your tinyurl-service, so it is reachable from all subdomains and then add the following code on top of your index.php file, right after the <?
     $id=eregi_replace("YOURDOMAIN\.COM","",$_SERVER[HTTP_HOST]);if(!$id=="") {$id=eregi_replace("\.$","",$id);header("Location: http://YOURDOMAIN.COM/$id");die;}
    Code (markup):
    then add the code in the tinyurl-script that the url that´s given to the user also lists offers the subdomain variant.


    but be aware, that the subdomain-solution is slightly slower, since each new-called subdomain creates also another DNS-request at the users end.
     
    happpy, Oct 31, 2008 IP
  4. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sweet - tnx for the idea. Gonna play w/ it a bit tonight.
     
    rob_v, Nov 1, 2008 IP
    happpy likes this.