【问题标题】:Pointing a Sub-domain to a sub directory将子域指向子目录
【发布时间】:2015-06-08 04:58:58
【问题描述】:

使用 Laravel,我在名为 webpage 的子目录中生成 php 页面。我想允许用户使用子域访问页面。例如,我在webpage 目录中生成了一个文件whoami.php。如果人们尝试访问whoami.domain.com,他可以看到whoami.php。我在webpage 中创建了一个index.php,它可以提取别名并显示文件。我需要的想法是将子域传递给 index.php 文件而不重定向。我写的htaccess如下

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    #sub domain custom part
        RewriteCond %{HTTPS} off
        RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
        RewriteRule ^(.*)$ http://domain.com/webpage/%1/$1 [L,NC,QSA]
</IfModule>

但我正在重定向到http://domain.com/webpage/whoami

我试图清楚地提出我的要求并粘贴我尝试过的内容。你能帮我想想办法吗?

编辑 1 根据@Starkeen 的回答,我现在在重定向循环中尝试了以下代码 (http://whoami.domain.com/webpage/whoami/webpage/whoami/......)。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

        #sub domain
        RewriteCond %{HTTPS} off
        RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
        RewriteRule ^(.*)$ /webpage/%1/$1 [L,NC,R]

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

【问题讨论】:

    标签: php .htaccess redirect laravel-5


    【解决方案1】:

    要将子域重定向到子文件夹,您可能需要将以下内容放在“RewriteEngine On”行之后:

     RewriteCond %{HTTP_HOST} ^(sub)\.domain\.com$ 
     RewriteRule ^(.*)$ /webpage/%1/$1 [NC,R,L]
    

    【讨论】:

    • 我现在在重定向循环中尝试了您的解决方案。我已经用我所做的代码更新了这个问题。你能检查一下吗。谢谢
    【解决方案2】:

    我得到了一个非常简单的解决方案。由于我正在使用 Laravel 并且已经使用 index.php 作为引导程序,因此我在那里编写了一些 PHP 代码来实现我的目标。有需要的小伙伴可以使用。如果我的解决方案不够好,请给我建议。谢谢

    我的解决方案

    $link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $link = str_replace(array('http://', 'https://', 'www.'), '', $link);
    
    $splitted = explode(".", $link);
    
    if($splitted[0] != 'domain'){
        $file_name = $splitted[0].'.php';
        $file_loc = 'webpage/'.$file_name;
    
        if(file_exists($file_loc)) {
            include($file_loc);
        } else {
            header('Location: http://domain.com/not-found');
        }
    
        die();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 2011-08-05
      • 2014-02-25
      • 2019-04-29
      • 2014-10-30
      • 2016-07-21
      • 1970-01-01
      • 2013-12-29
      • 2010-10-17
      相关资源
      最近更新 更多