Kohana3: Different .htaccess rewritebase and kohana base_url for dev and production environment
Asked Answered
B

3

10

In my bootstrap.php I have the following:

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;
}

In .htaccesshave this:

# Installation directory
RewriteBase /kohana/

This means that if I just upload my kohana application, it will break because the RewriteBase in the .htaccess file will be wrong. Is there a way I can have a conditional in the .htaccess file similar to the one I have in the bootstrap so that it will use the correct RewriteBase?

Blockbuster answered 23/3, 2010 at 16:7 Comment(0)
P
8

I don't think there is a way to have a conditional RewriteBase. The only way in Apache that comes to mind is putting the RewriteBase directive into a <Location> tag but that is only available in httpd.conf itself, not in .htaccess files.

What I usually do in such cases is define a different AccessFileName in the local environment, for example htaccess.txt. That file will contain the local rewrite rules, while .htaccess contains the server side ones.

Plastometer answered 23/3, 2010 at 16:22 Comment(2)
Clever that last one there... Do I set the AccessFileName in the httpd.conf? Can I set them per virtual host too?Blockbuster
AccessFileName actually supports more than one filename, so I could just add the following: AccessFileName local.htaccess .htaccess and regular .htaccess files would still work :)Blockbuster
S
5

I ran across this while looking for a similar solution. While I didn't get RewriteBase to set conditionally, I did get a similar effect by setting an environment variable and using that in the following rules. Here's an example of what I mean.

# 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]
Stephaniestephannie answered 21/4, 2010 at 21:43 Comment(3)
Interesting... not sure I get what happens there though :pBlockbuster
Well the base directory that I would normally set RewriteBase to get's saved to an environment variable ($REWRITEBASE). In the RewriteCond and RewriteRule, that variable is expanded and prefixed to the result. For example - if the server_name is localhost, the RewriteRule becomes .* /local_dir/index.php$0 [PT].Stephaniestephannie
I should note too that this is on Apache 2.2. I'm not sure how earlier versions would handle this.Stephaniestephannie
G
2

If your htaccess file is in the installation base (ie next to index.php), then you don't particularly need RewriteBase, i leave it out of my applications and they work beautifully. Even with two Kohana installs, one inside the other directory.

Gregoriagregorian answered 11/7, 2010 at 23:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.