views:

59

answers:

3

I would like to set up my codeigniter projects so I can use it in any folder on my webserver. Unfortunately codeigniter requires me to specify the location in several variables of the config. This is a problem for portability.

There are 3 places where I see this:

1) $config['base_url'] 
2) $config['base_path'] 
3) .htaccess RewriteRule

So far, I have found the solution to (1) here: http://codeigniter.com/wiki/Automatic_base_url/

I am looking for solutions to (2) and (3), but most importantly (3). Here is an example situation:

I set .htaccess to have the RewriteRule as follows:

RewriteRule ^(.*)$ /folderA/index.php/$1 [L]

Then I go into my repo and check out the project to another folder to make some separate changes. Now I have to update .htaccess to show the following:

RewriteRule ^(.*)$ /folderB/index.php/$1 [L]

I would like to set it so I dont have to make any config changes when I check out another copy.

A: 

Im not sure you can make the .htaccess portable, in my experience the mod_rewrite stuff is really touchy to begin with, let alone making it portable.

As for (2) im not entirely sure this is required anymore. However you could make a define at the top of index.php that gets the location of the file and saves it, then pull out the define later in the config file.

Aren
+1  A: 

Since it appears that you are running Apache, the easiest thing would probably be to set up virtual hosts and use the "automatic base URL" trick that you found. You could have localhost.folderA have document root C:\...\folderA and localhost.folderB have document root C:\...\folderB, for example.

From a quick Internet search for "Apache how to set up virtual hosts", this page looks promising for complete instructions on how to set up Apache virtual hosts on Windows. Try following its instructions with site1.local replaced with localhost.folderA and site2.local replaced with localhost.folderB.

Daniel Trebbien
Thanks for the tip. But the reason I want to do this is so that I can stop having to play with config files.
Jono
A: 

For your base path you can have:

$config['base_path'] = $_SERVER['DOCUMENT_ROOT'];

Or

$config['base_path'] = dirname(FCPATH) . '/';

And for the base URL

$config['base_url'] = 'http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER["SCRIPT_NAME"]);

Edit:

$config['base_url'] =  dirname($_SERVER["SCRIPT_NAME"]);

This is cleaner but uses relative URL's

DRL