【问题标题】:Dynamic sub domains using mod rewrite and htaccess使用 mod rewrite 和 htaccess 的动态子域
【发布时间】:2014-04-14 00:16:23
【问题描述】:

好的,我已经从不同的地方尝试了多个这样的例子,但似乎无法让它发挥作用。也许我在这里遗漏了一些东西......

  • 我已经使用 *.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]

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    使用以下链接解决问题:

    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(用您在第 4 行的信息替换用户名和 example.com)

    <?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");
    
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多