在我的 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应用程序,它会因为RewriteBase而中断 .htaccess
文件会出错。有没有办法让我有条件的 .htaccess
文件类似于我在bootstrap中的文件,以便它将使用正确的RewriteBase?
我不认为有条件的方法 RewriteBase
。想到Apache的唯一方法是放入 RewriteBase
指令进入 <Location>
标签,但仅在httpd.conf本身可用,而不在.htaccess文件中。
在这种情况下我通常做的是定义一个不同的 AccessFileName 例如,在当地环境中 htaccess.txt
。该文件将包含本地重写规则 .htaccess
包含服务器端的。
我不认为有条件的方法 RewriteBase
。想到Apache的唯一方法是放入 RewriteBase
指令进入 <Location>
标签,但仅在httpd.conf本身可用,而不在.htaccess文件中。
在这种情况下我通常做的是定义一个不同的 AccessFileName 例如,在当地环境中 htaccess.txt
。该文件将包含本地重写规则 .htaccess
包含服务器端的。
我在寻找类似的解决方案时遇到了这个问题。虽然我没有有条件地设置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]
如果您的htaccess文件位于安装基础中(即index.php旁边),那么您并不特别需要RewriteBase,我将其从我的应用程序中删除,并且它们可以很好地工作。即使有两个Kohana安装,一个在另一个目录中。