WAMP Subdomain Localhost Guide 1.2.0
Ok everyone I have been away for a while and now I am back I have updated the Process to work with the LATEST Versions of WAMP and have tested on XP, Vista and 7 32&64bit versions.

Here is a little something I did to make the subdomain creation for LOCALHOST. MAKE SURE TO DO BACKUP OF YOUR FILES BEFORE YOU EDIT THEM!


Download and save the file from here
[http://www.btt-scripts.com/WAMP_Localhost_update.zip]
############
STEP 1
############

create a folder called 'subdomains' in C:/WAMP/apache2/conf/

Create 2 folders called 'access' & 'error' ( this is for having a place for seperate logs files for local subdomains) in C:/WAMP/Logs/

############
STEP 2
############

In WAMP enable the 'vhost_alias_module'

NEXT Uncomment the Virtual hosts option in the 'httpd.conf' file # Virtual hosts Include conf/extra/httpd-vhosts.conf

open httpd-vhost.conf and edit to look like below or delete all the content but not the file itself. [code]

# 
# Virtual Hosts 
# 
# If you want to maintain multiple domains/hostnames on your 
# machine you can setup VirtualHost containers for them. Most configurations 
# use only name-based virtual hosts so the server doesn't need to worry about 
# IP addresses. This is indicated by the asterisks in the directives below. 
# 
# Please see the documentation at 
# <URL:[httpd.apache.org]; 
# for further details before you try to setup virtual hosts. 
# 
# You may use the command line option '-S' to verify your virtual host 
# configuration. 

# 
# Use name-based virtual hosting. 
# 
NameVirtualHost *:80 

# 
# VirtualHost example: 
# Almost any Apache directive may go into a VirtualHost container. 
# The first VirtualHost section is used for all requests that do not 
# match a ServerName or ServerAlias in any  block. 
# 
[/code]

############ STEP 3 ############ Next open wampmanagertpl and add this to the or REPLACE the file with the one included in the zip file.

[code]

  [Menu.Left] 
Type: separator; Caption: "Localhost Subdomains" 
Type: submenu; Caption: "Subdomain directories"; SubMenu: subdomain_dir; Glyph: 3 
[/code]

Once you have done that you need to add this to the very bottom of the wampmanager.tpl file (TEMPLATE FILE)

[code]

  [add_subdomain] 
Action: run; FileName: "${c_phpExe}";Parameters: "-c . add_subdomain.php";WorkingDir: "${c_installDir}/scripts"; Flags: waituntilterminated
Action: run; FileName: "${c_phpCli}";Parameters: "refresh.php";WorkingDir: "${c_installDir}/scripts"; Flags: waituntilterminated
Action: service; Service: wampapache; ServiceAction: restart;
Action: resetservices 
Action: readconfig; 
              
[subdomain_dir] 
Type: separator; Caption: "Subdomain directories" 
Type: item; Caption: "Add an subdomain"; Action: multi; Actions: add_subdomain;Glyph : 8 
Type: separator 
;WAMPSUBDOMAINLOOP
[/code]

Save the file

############
STEP4
############

Edit the config.inc.php file located in the scripts folder or REPLACE with the one included in the zip

ADD THIS BELOW LINE 48 or 49

[code]

  // Subdomain Configs
$scriptsDir = $c_installDir.'/scripts/';
$httpconf_file	= $c_apacheVersionDir.'/apache'.$wampConf['apacheVersion'].'/'.$wampConf['apacheConfDir'].'/extra/httpd-vhosts.conf';
$wampservertpl_file = $templateFile;
$wampserverini_file = $wampserverIniFile; 
$conffile_dir = $c_apacheVersionDir.'/apache'.$wampConf['apacheVersion'].'/'.$wampConf['apacheConfDir'].'/subdomains/'; 
$hosts = 'C:/Windows/system32/drivers/etc/hosts'; 
[/code]

############
STEP5
############

Edit the refresh.php located in the scripts folder or replace it with the one in the zip file

AROUND LINE 352 $tpl = str_replace(';WAMPAPACHE_MODSTART',$httpdText,$tpl); ADD THIS BELOW THAT.

[code]

  
// ************************
// Subdomain Apache
// AN ENCAPSULATED VERSION

function nvh($conf, $truncate=FALSE){
	// EXPLODE INTO AN ARRAY OF LINES
	$arry = explode(PHP_EOL, $conf);
	$host = FALSE;
	// ITERATE OVER THE ARRAY
	foreach ($arry as $line)    {
		// ELIMINATE EXCESS WHITESPACE
		$line = preg_replace('/\s\s+/', ' ', $line);
		$pair = explode(' ', $line);
		if ($pair[0] == 'NameVirtualHost')        {
			$host = $pair[1];
			break;
		}
	}
	// IF WE WANT TO REMOVE THE PORT
	if ($truncate)    {
		$poz = strpos($host, ':');
		if ($poz !== FALSE) $host = substr($host,0,$poz);
	}
	// RETURN
	return trim($host);
	}
	if ($handle = opendir($conffile_dir)) {
		while (false !== ($file = readdir($handle)))    {
		if ($file != "." && $file != ".." && strstr($file,'.conf'))        {
		$subDirContents[] = $file;
		}
	}
	closedir($handle);
}
$myreplace = $myreplacemenu = $mydeletemenu = '';
foreach ($subDirContents as $one_sub){
	$mypattern = ';WAMPSUBDOMAINLOOP';
	$newsub_dir = str_replace('.conf','',$one_sub);
	$sub_contents = @file_get_contents ($conffile_dir.$one_sub);
	preg_match('|^Alias /'.$newsub_dir.'/ "(.+)"|',$sub_contents,$match);
	        
	$ip_dest = nvh($sub_contents,TRUE);
	
	if (isset($match[1]))
	$newsub_dest = $match[1]; 
	else
	$newsub_dest = NULL; 
	$myreplace .= 'Type: submenu; Caption: "http://'.$newsub_dir.'/"; SubMenu: subdomain_'.str_replace(' ','_',$newsub_dir).'; Glyph: 3
	';
	$myreplacemenu .= '
	;start_subdomain_'.ereg_replace(' ','_',$newsub_dir).'_menu
	[subdomain_'.ereg_replace(' ','_',$newsub_dir).']
	Type: separator; Caption: "'.$newsub_dir.'"
	Type: item; Caption: "Run '.$newsub_dir.'"; Glyph: 6; Action: run; FileName: "C:\Program Files\Internet Explorer\iexplore.exe"; Parameters: "http://'.$newsub_dir.'/"
	Type: item; Caption: "Edit subdomain"; Glyph: 6; Action: multi; Actions: edit_subdomain_'.ereg_replace(' ','_',$newsub_dir).'
	Type: item; Caption: "Edit .htaccess"; Glyph: 6; Action: run; FileName: "notepad.exe"; parameters: "'.$newsub_dest.'.htaccess"
	Type: item; Caption: "Delete subdomain"; Glyph: 6; Action: multi; Actions: delete_subdomain_'.ereg_replace(' ','_',$newsub_dir).'
	;end_subdomain_'.ereg_replace(' ','_',$newsub_dir).'_menu
	;add_menu_subdomain
	';
	$mydeletemenu .= '
	;start_subdomain_'.ereg_replace(' ','_',$newsub_dir).'_delete
	[delete_subdomain_'.ereg_replace(' ','_',$newsub_dir).']
	Action: run; FileName: "'.$c_phpExe.'";Parameters: "-c . delete_subdomain.php '.ereg_replace(' ','-whitespace-',$newsub_dir).' '.ereg_replace(' ','-whitespace-',$ip_dest).'";WorkingDir: "'.$c_installDir.'/scripts/"; Flags: waituntilterminated
	Action: run; FileName: "'.$c_phpCli.'";Parameters: "refresh.php";WorkingDir: "'.$c_installDir.'/scripts"; Flags: waituntilterminated
	Action: service; Service: wampapache; ServiceAction: restart;
	Action: resetservices
	Action: readconfig;
	[edit_subdomain_'.ereg_replace(' ','_',$newsub_dir).']
	Action: run; FileName: "notepad.exe"; parameters:"'.$conffile_dir.$newsub_dir.'.conf"; Flags: waituntilterminated
	Action: service; Service: wampapache; ServiceAction: restart;
	Action: resetservices
	Action: readconfig;
	;end_subdomain_'.ereg_replace(' ','_',$newsub_dir).'_delete
	';
}
$tpl = str_replace($mypattern,$myreplace.$myreplacemenu.$mydeletemenu,$tpl);
        
[/code]

##################### REQUIRED ###################
THIS SECTION IS UP TO YOU IF YOU WANT TO JUST UPLOAD THE "add_subdomain.php & delete_subdomain.php" OR CREATE THEM FROM SCRATCH.

##########################
add_subdomain.php content
##########################

[code]

  <?php
//v1.2.0 by James Neeley for Subdomains for localhost



require 'config.inc.php';

// Echo Enter in Subdomain Name
echo '# Subdomain Creation Tool for WAMP  
# Modded by James Neeley

########################
# Step 1 - Name
########################

Enter your subdomain name. 

NOTE: It would be wise to include the localhost in the subdomain name.

For example, 

\'test\'
or
\'test.localhost\'

would create an subdomain for the url 

http://test/
or
http://test.localhost/

: ';
$newalias_dir = trim(fgets(STDIN));
$newalias_dir = trim($newalias_dir,'/\'');
	if (is_file($conffile_dir.$newalias_dir.'.conf')){
    	echo '
			Subdomain already exists. Press Enter to exit...';
    		trim(fgets(STDIN));
    	exit();
	}

	if ($newalias_dir == ''){
    	echo '
			Subdomain not created. Press Enter to exit...';
    		trim(fgets(STDIN));
    	exit();
	}

// Echo subdomain destination location
echo '





########################
# Step 2 - Files location
########################


Enter the destination of your subdomain.
For example, 

\'c:/www/subdomain/\'

would make http://'.$newalias_dir.'.localhost/ point to 

c:/www/subdomain/

: ';
$newalias_dest = trim(fgets(STDIN));
	if ($newalias_dest[strlen($newalias_dest)-1] != '/')
    	$newalias_dest .= '/';
		if (!is_dir($newalias_dest)) {
    		echo '
				This directory doesn\'t exist.
			';
    		$newalias_dest = '';
		}
		if ($newalias_dest == ''){
    		echo '
				Subdomain not created. Press Enter to exit...';
   				trim(fgets(STDIN));
    			exit();
		}

// Echo IP Statement for Include
echo '





########################
# Step 3 - IP Address
########################


Enter the IP of your Subdomain.  

NOTE: You must enter a IP or your subdomains will not work properly. 
If not, a default IP of 127.0.0.1 will be entered

For example, 

\'127.0.0.1\'

would make http://127.0.0.1/ point to 

127.0.0.1

: ';
	$ip_dest = trim(fgets(STDIN));
	if ($ip_dest == ''){
    		$ip_dest="127.0.0.1";
		}
		
// Echo Port Statement for Include
echo '





########################
# Step 4 - Port Address
########################


Enter the Port of your Subdomain.  

NOTE: You must enter a Port or your subdomains will not work properly. 
If not, a default Port of 80 will be entered

For example, 

\'80\'

would make http://'.$newalias_dir.':80/ point to 

'.$newalias_dir.':80

: ';
	$port_dest = trim(fgets(STDIN));
	if ($port_dest == ''){
    		$port_dest="80";
		}

// Echo Admin Email Statement for Include
echo '





########################
# Step 5 - Email Address
########################


Enter the Admin Email of your Subdomain.  

You can enter in an admin email address for your subdomain, however
this not required. If you enter nothing it will be commented out in 
the subdomain config file.

Example: admin@'.$newalias_dir.'

: ';
	$email_dest = trim(fgets(STDIN));
	if ($email_dest == ''){
    		//$email_id="#ServerAdmin admin@$newalias_dir";
    		$email_id="ServerAdmin admin@$newalias_dir";
	}	else	{
			$email_id="ServerAdmin $email_dest";
	}
	
// Echo Access Log Statement for Include
echo '





########################
# Step 6 - Access Log Path
########################


Enter the Access Log path of your Subdomain.  

You can enter in the access log path for your subdomain, however 
this not required. If you enter nothing it will be commented out in the
subdomain config file.

NOTE: If you enter a path, that folder must exist first or apache will not
re-start correctly.

Example: \''.$c_installDir.'/Logs/access/\'

Will output to this:

'.$c_installDir.'/Logs/access/'.$newalias_dir.'-access_log common

: ';
	$access_dest = trim(fgets(STDIN));
	if ($access_dest == ''){
    		//$access_id="#CustomLog $newalias_dest$newalias_dir-access_log common";
    		$access_id="CustomLog ".$c_installDir."/logs/access/$newalias_dir-access_log common";
	}	else	{
			$access_id="CustomLog $access_dest$newalias_dir-access_log common";
	}
	
// Echo Error Log Statement for Include
echo '





########################
# Step 7 - Error Log Path
########################


Enter the Error Log path of your Subdomain.  

You can enter in the error log path for your subdomain, however 
this not required. If you enter nothing it will be commented out in the
subdomain config file.

NOTE: If you enter a path, that folder must exist first or apache will not
re-start correctly.

Example: \''.$c_installDir.'/Logs/error/\'

Will output to this:

'.$c_installDir.'/Logs/error/'.$newalias_dir.'-access_log common

: ';
	$error_dest = trim(fgets(STDIN));
	if ($error_dest == ''){
    		//$error_id="#ErrorLog $newalias_dest$newalias_dir-error_log";
    		$error_id="ErrorLog ".$c_installDir."/logs/error/$newalias_dir-error_log";
	}	else	{
			$error_id="ErrorLog $error_dest$newalias_dir-error_log";
	}
	

// Write new subdomain vhost.conf file.
$newconffile_contents = '# Subdomain for  '.$newalias_dir.' 
NameVirtualHost '.$ip_dest.':'.$port_dest.'

'.$email_id.'
DocumentRoot '.$newalias_dest.'
ServerName '.$newalias_dir.':'.$port_dest.'
ServerAlias '.$newalias_dir.'
'.$error_id.'
'.$access_id.'
 

Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all

';

// Write to httpd=vhosts.conf file
@file_put_contents($conffile_dir.$newalias_dir.'.conf',$newconffile_contents) or die ("unable to create conf file");
$httpdconf_add = '
Include "'.$conffile_dir.$newalias_dir.'.conf"';
@file_put_contents($httpconf_file,$httpdconf_add,FILE_APPEND) or die ("http.conf file not found");

// Write to hosts file

$hostsconf_add = '
'.$ip_dest.'	'. $newalias_dir.'';
@file_put_contents($hosts,$hostsconf_add,FILE_APPEND) or die ("hosts file not found");

echo '
Subdomain created. Press Enter to exit...';
    trim(fgets(STDIN));
    exit();
?>
[/code]

##########################
delete_subdomain.php content
##########################

[code]

  <?php
//v1.2.0 by James Neeley for Subdomains for localhost

require 'config.inc.php';

echo '

Do you really wanty to delete this subdomain?
Type \'yes\' to confirm : ';
$confirm =    trim(fgets(STDIN));
$confirm =    trim($confirm ,'\'');

if ($confirm == 'no') {
	echo'
	Subdomain not deleted. Press Enter to exit...';
	trim(fgets(STDIN));
    exit();
}
if ($confirm == 'yes') {
	echo'
	
	
Deleting Subdomain Conf Files



';	
    echo ' 
Deleted conf file    '.ereg_replace('-whitespace-',' ',$_SERVER['argv'][1]).'.conf file';
    unlink ($conffile_dir.ereg_replace('-whitespace-',' ',$_SERVER['argv'][1]).'.conf');
    
    echo ' 
Deleted conf httplink    '.$conffile_dir.ereg_replace('-whitespace-',' ',$_SERVER['argv'][1]).'.conf for the httpd-vhost.conf file';

    $myhttpd = file_get_contents($httpdconf_file);    
    $httpdline = 'Include "'.$conffile_dir.ereg_replace('-whitespace-',' ',$_SERVER['argv'][1]).'.conf"';
	$myhttpd = ereg_replace ($httpdline,'',$myhttpd);
    file_put_contents($httpdconf_file,$myhttpd);
    
    echo ' 
Deleted hosts record    '.ereg_replace('-whitespace-',' ',$_SERVER['argv'][2]).'	'.ereg_replace('-whitespace-',' ',$_SERVER['argv'][1]).'';
    $myhosts = file_get_contents($hosts);    
    $hostsline = ''.ereg_replace('-whitespace-',' ',$_SERVER['argv'][2]).'	'.ereg_replace('-whitespace-',' ',$_SERVER['argv'][1]).'';
    $myhosts = ereg_replace ($hostsline,'',$myhosts);
    file_put_contents($hosts,$myhosts);
    
    echo '
   
Subdomain deleted. Press Enter to exit...';
    trim(fgets(STDIN));
    exit();
}
else
{
echo'
Subdomain not deleted. Press Enter to exit...';
trim(fgets(STDIN));
exit();
}
?>

    
[/code]

Restart WAMPSERVER AND YOU ARE DONE!
 
Copyright © 2003-2010 BTT-Scripts Inc. All rights reserved. Powered by: BTT-Scripts Inc.