Dynamic Subdomains

Before you get dynamic subdomains, you must first contact DreamHost, and have them turn on Wildcard DNS.

Next you will need to make an .htaccess file located in the root directory of your domain (NOT subdomain), and place the following code in it (Replace example.com with your site on all appropriate lines and leave any \ before periods): Options +FollowSymLinks

RewriteEngine On RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC] RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC] RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
 * 1) Fix missing trailing slashes.

RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC] RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
 * 1) Rewrite sub domains.

RewriteRule ^(.*)$ subdomains/%2/$1 [QSA,L]
 * 1) Choose one of the following lines:
 * 1) RewriteRule ^(.*)$ viewSubdomain.php?subdomain=%2&file=$1 [QSA,L]

The last two lines may be hard to understand, so here is an explanation of each:

RewriteRule ^(.*)$ subdomains/%2/$1 [QSA,L] This statement tells Apache to look for the subdomain in the folder subdomains within your "Main" site. So the two basic ways to access the site would be as follows:
 *  http://example.com/subdomains/test/  -- This requires Absolute linking
 *  http://test.example.com 

RewriteRule ^(.*)$ viewSubdomain.php?subdomain=%2&file=$1 [QSA,L] This statement tells Apache to find the file viewSubdomain.php in your "Main" domain's root folder. This then requires some code to display the page. This method is really only useful if your are running a web host, and would like to make a banner on the top of all the pages, or pop-up in every subdomain.

Example PHP Code (Replace username and example.com with your info (line 4)): <?php set_time_limit(20); $fileTypes = array('index.php','index.html','index.htm'); $file = '/home/username/example.com'; $err = FALSE; if(isset($_GET['file'])){ if(!file_exists($file.'/subdomains/'.$_GET['subdomain'].'/index.php')){ if(!file_exists($file.'/subdomains/'.$_GET['subdomain'].'/index.html')){ if(!file_exists($file.'/subdomains/'.$_GET['subdomain'].'/index.htm')){ $err = TRUE; }			}		}		if(empty($_GET['file'])&&file_exists($file.'/subdomains/'.$_GET['subdomain'].'/index.php')){ $f = 'index.php'; $s = 'index.php'; }		elseif(empty($_GET['file'])&&file_exists($file.'/subdomains/'.$_GET['subdomain'].'/index.htm')){ $f = 'index.htm'; $s = 'index.htm'; }		elseif(empty($_GET['file'])&&file_exists($file.'/subdomains/'.$_GET['subdomain'].'/index.html')){ $f = 'index.html'; $s = 'index.html'; }else{ $s = $_GET['file']; $f = $_GET['file']; $err = FALSE; }	}else{ $f = $_GET['file']; $s = $_GET['file']; }	$file .= '/subdomains/'.$_GET['subdomain'].'/'.$f; if(file_exists($file)&&!$err){ if(preg_match("~^(.*).(css|js|jpeg|jpg|gif|png|swf)$~",$file,$matches)){ if($matches[2]=='css'){ header("Content-type: text/css"); }elseif($matches[2]=='js'){ header("Content-type: text/javascript"); }elseif($matches[2]=='jpeg'){ header("Content-type: image/jpeg"); }elseif($matches[2]=='jpg'){ header("Content-type: image/jpg"); }elseif($matches[2]=='gif'){ header("Content-type: image/gif"); }elseif($matches[2]=='png'){ header("Content-type: image/png"); }elseif($matches[2]=='swf'){ header("Content-type: application/x-shockwave-flash"); }			readfile($file); }else{ include $file; }	}else{ header("HTTP/1.0 404 Not Found");

} ?>

Next, you will need to make a folder called subdomains.

To add your first subdomain, simply make a new folder within subdomains such as "newsubdomain", and then just go to  http://newsubdomain.example.com