Thursday, June 18, 2009

Auto Generate Subdomains form a Subdirectory of a Site

this script will automatically(on the fly) translate/resolve subdomian into dedicated folder path.
without touching web server config(

  • only touch the config for 1st time config only la...).

    eg:

    main domain name:
      http://mysite.com

    main vhost doc root:
      /usr/local/www

    Folder inside:
      /usr/local/www/brother
      /usr/local/www/sister
      /usr/local/www/mother
      /usr/local/www/father
      /usr/local/www/inlaw
      /usr/local/www/cucu
      /usr/local/www/cicit
      /usr/local/www/oneng2
      ........

    so

    when browser open url:
      http://mysite.com/

    it will display:

  • brother
  • sister
  • mother
  • father
  • inlaw
  • cucu
  • cicit
  • oneng2

    But when browser open url:
      http://brother.mysite.com/

    it will display:
      content inside folder 'brother', and can view everything inside this folder.

    The Script:

    #!/usr/bin/perl
    ########################################
    use CGI::Carp qw(fatalsToBrowser set_message);
    BEGIN {sub handle_errors { my $msg = shift;
    print qq|Server Error
    

    Web Server Error!

    $msg

    Please inform the mbek about this error.\n|;} set_message(\&handle_errors);} ######################################## use CGI; ## Set a few variables ############################################################## my $hostname = "site.williams"; my $perlexe = "c:/programs/perl/bin/perl.exe"; my $phpexe = "C:/Programs/server/php/php.exe"; ############################################################## if (exists $ENV{'PATH_INFO'}) { $uri_info = $ENV{'PATH_INFO'}; $uri_info =~ s/alias\.pl//; $uri_info =~ s/^\///g; } elsif (exists $ENV{'DOCUMENT_URI'}) { $uri_info = $ENV{'DOCUMENT_URI'}; $member_url =~ s/^http:\/\/.+?\///; $uri_info =~ s/^\///g; $uri_info =~ s/^$member_url//; } else { $uri_info = ''; } my $hosts = $ENV{'HTTP_HOST'}; my $sysdir = $ENV{'DOCUMENT_ROOT'}; my $script = $ENV{'DOCUMENT_NAME'}; my ($hostid,$host) = split /$hostname/, $hosts, 2; $hostid =~ s/\.$//; $sysdir =~ s/\/$//; $sysdir .= "/$hostid" if $hostid; $uri_info =~ s/$script//; $uri_info =~ s/^\///; $uri_info =~ s/\/$//; my $file = "$sysdir"; $file .= "/$uri_info" if $uri_info; $file =~ s/\/$//; if(!-e "$file"){ print "Content-type: text/html\n\n"; print "Invalid Path ($file)"; exit; } if(-d "$file"){ print "Content-type: text/html\n\n"; # load all files of the "data/" folder # into the @files array opendir(DIR, "$file"); my @files = readdir(DIR); closedir(DIR); if(@files){ print "

      "; foreach $file (@files) { next if ($file eq "." or $file eq ".."); my $folderpath; if($uri_info){ $folderpath = '/' . $uri_info . '/' . $file; }else{ $folderpath = '/' . $file; } $folderpath = "/". $script . $folderpath if $script && $uri_info ne $script; print "
    • $file
    • "; } print "
    "; }else{ print "Folder is empty"; } exit; }else{ if($file =~ /\.pl$/i || $file =~ /\.cgi$/i){ #handle perl/CGI my $result = `$perlexe $file`; print "$result"; exit; }elsif($file =~ /\.php$/i){ #handle PHP my $result = `$phpexe $file`; print "$result"; exit; }else{ use File::MMagic; my $mime = File::MMagic->new(); print "Content-type: " . $mime->checktype_filename($file) . "\n"; #The Content-Disposition will generate a prompt to save the file. If you want #to stream the file to the browser, comment out the following line. #print "Content-Disposition: attachment; filename=$filename\n"; print "\n"; binmode STDOUT; if($mime->checktype_filename($file) =~ /image\/png/i){ use GD; my $contents = GD::Image->new( $file ); print $contents->png; }else{ my $fh = new IO::File($file, "r"); my $contents = get_contents($fh); print $contents; } exit; } } exit; ############################ sub get_contents { ############################ my $in = shift; my $ret = ""; while (<$in>) { $ret.= $_; } return $ret; }

    and this apache httpd-vhost.conf

    <VirtualHost *:80>
            ServerAdmin [email protected]
            DocumentRoot "/usr/local/www"
            ServerName mysite.com
            ServerAlias mysite.com
    
            ErrorLog "logs/mysite.com-error.log"
            CustomLog "logs/mysite.com-access.log" common
            Options +Includes
            AddHandler server-parsed .html
    
            <Directory "/usr/local/www">
                    AllowOverride None
                    Options Indexes FollowSymLinks ExecCGI
                    Order allow,deny
                    Allow from all
            </Directory>
            
            RewriteEngine On
    	RewriteRule /(.*)$ /alias.pl/$1 [L]
    </VirtualHost>
    

    use at your own risk...

    happy coding..

  • No comments: