代码之家  ›  专栏  ›  技术社区  ›  Svish

Kohana3:不同。开发和生产环境的htaccess rewritebase和Kohana base_url

  •  11
  • Svish  · 技术社区  · 15 年前

    在我的 bootstrap.php 我有以下资料:

    if($_SERVER['SERVER_NAME'] == 'localhost')
        Kohana::$environment = 'development';
    else
        Kohana::$environment = 'production';
    
    ...
    
    switch(Kohana::$environment)
    {
        case 'development':
            $settings = array('base_url' => '/kohana/', 'index_file' => FALSE);
            break;
    
        default:
            $settings = array('base_url' => '/', 'index_file' => FALSE);
            break;
    }
    

    .htaccess 有这样的:

    # Installation directory
    RewriteBase /kohana/
    

    这意味着,如果我只是上传我的kohana应用程序,它将崩溃,因为 HTAccess 文件将出错。有没有办法让我在 HTAccess 类似于引导程序中的文件,以便使用正确的重写基?

    3 回复  |  直到 15 年前
        1
  •  9
  •   Pekka    15 年前

    我不认为有条件的 RewriteBase . 在apache中唯一想到的方法是 改写库 指令变成 <Location> 但这仅在httpd.conf本身中可用,而在.htaccess文件中不可用。

    在这种情况下,我通常做的是定义一个不同的 AccessFileName 例如,在本地环境中 htaccess.txt . 该文件将包含本地重写规则,而 .htaccess 包含服务器端的。

        2
  •  5
  •   mozillalives    15 年前

    我在寻找类似的解决方案时遇到了这个问题。虽然我没有让rewritebase按条件设置,但通过设置环境变量并在以下规则中使用它,我确实获得了类似的效果。这是我的意思的一个例子。

    # Set an environment variable based on the server name
    RewriteRule %{SERVER_NAME} localhost [E=REWRITEBASE:/local_dir/]
    RewriteRule %{SERVER_NAME} prodhost [E=REWRITEBASE:/prod_dir/]
    
    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{ENV:REWRITEBASE}%{REQUEST_FILENAME} !-f 
    RewriteCond %{ENV:REWRITEBASE}%{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !^static
    
    # Rewrite all other URLs to index.php/URL
    RewriteRule .* %{ENV:REWRITEBASE}index.php/$0 [PT]
    
        3
  •  2
  •   Chris White    15 年前

    如果你的htaccess文件在安装库中(即index.php旁边),那么你不需要特别的rewritebase,我把它放在我的应用程序之外,它们工作得很好。即使安装了两个kohana,其中一个在另一个目录中。

    推荐文章