2014-04-14 48 views
0

好吧我已經嘗試過多個來自不同地方的例子,但似乎無法讓它工作。也許我缺少的東西在這裏...使用mod rewrite和htaccess的動態子域

  • 我必須使用* .example.com的(它指向的文件夾中的public_html)
  • 子域我需要加載位於已啓用通配符子域在一個名爲users的文件夾中,例如該結構將是example.com/users/testuser,我需要調用testuser.example.com並讓它加載testuser文件夾中的內容(將是index.php文件)。

任何幫助,非常感謝。

的.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

問題通過以下鏈接解決:

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)