Kohana3:开发和生产环境的不同.htaccess rewritebase和kohana base_url

Svi*_*ish 11 php .htaccess kohana kohana-3

在我,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;
}
Run Code Online (Sandbox Code Playgroud)

.htaccess有这样的:

# Installation directory
RewriteBase /kohana/
Run Code Online (Sandbox Code Playgroud)

这意味着如果我只是上传我的kohana应用程序,它将会中断,因为.htaccess文件中的RewriteBase 是错误的.有没有办法我可以在.htaccess文件中有条件类似于我在引导程序中的条件,以便它将使用正确的RewriteBase?

Pek*_*ica 9

我不认为有条件的方法RewriteBase.想到Apache的唯一方法就是将RewriteBase指令放入<Location>标签中,但只能在httpd.conf本身中使用,而不能在.htaccess文件中使用.

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

  • AccessFileName实际上支持多个文件名,所以我可以添加以下内容:`AccessFileName local.htaccess .htaccess`和常规.htaccess文件仍然可以工作:) (6认同)

moz*_*ves 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]
Run Code Online (Sandbox Code Playgroud)