好的我已经从不同的地方尝试了多个这样的例子,但似乎无法让它发挥作用。也许我在这里错过了一些东西......
非常感谢任何帮助。
的.htaccess
RewriteEngine On
RewriteCond %{http_host} ^example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.example.com/$2 [QSA,NC,R,L]
答案 0 :(得分:0)
使用以下链接解决问题:
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(将用户名和example.com替换为第4行的信息)
<?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");
}
?>