10

在我的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文件中的 RewriteBase 错误而中断。有没有一种方法可以让.htaccess文件中的条件类似于我在引导程序中的条件,以便它使用正确的 RewriteBase?

4

3 回答 3

8

我不认为有办法有一个条件RewriteBase。在 Apache 中想到的唯一方法是将RewriteBase指令放入<Location>标记中,但这仅在 httpd.conf 本身中可用,在 .htaccess 文件中不可用。

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

于 2010-03-23T16:22:52.597 回答
5

我在寻找类似的解决方案时遇到了这个问题。虽然我没有让 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]
于 2010-04-21T21:43:34.293 回答
2

如果您的 htaccess 文件在安装库中(即在 index.php 旁边),那么您就不需要特别需要 RewriteBase,我将它排除在我的应用程序之外并且它们运行良好。即使安装了两个 Kohana,一个在另一个目录中。

于 2010-07-11T23:40:25.877 回答