問題通過以下鏈接解決:
http://wiki.dreamhost.com/Dynamic_Subdomains
的方式我這個結構:
的public_html /子域
的public_html /。 htaccess
public_html/viewSubdomain.php
下面的代碼是從上面的鏈接拉。
.htaccess文件
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
# Fix missing trailing slashes.
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]
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
#Choose one of the following lines:
#RewriteRule ^(.*)$ subdomains/%2/$1 [QSA,L]
RewriteRule ^(.*)$ viewSubdomain.php?subdomain=%2&file=$1 [QSA,L]
viewSubdomain.php
<?php
set_time_limit(20);
$fileTypes = array('index.php','index.html','index.htm');
$file = '/home/username/example.com'; //replace this info with your path
$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");
}
?>
(與4號線的信息更換用戶名和example.com)