How to configure when putting CodeIgniter into existing site

#1
[eluser]Unknown[/eluser]
I also posted this to stackoverflow, but I think the real experts are here!

I have an existing "straight" PHP site at http://www.mysites.com/site1/ and want to migrate it to CodeIgniter over time. I know there are a few ways to do this, but I'd like to leave the existing site alone and just put the CodeIgniter folder (CI) in the root directory (site1), then redirect requests to it if the file or directory doesn't exist.

So this .htaccess file is in the site1 folder:

RewriteEngine on
RewriteBase /site1/CI/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ index.php/$1 [L]

and this .htaccess file is in the site1/CI folder (needed?):

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ index.php/$1 [L]

and the CodeIgniter config.php file contains:

$config['base_url'] = 'http://www.mysites.com/site1/CI/';


This works fine if I try to go to a location on the existing site (e.g., http://www.mysites.com/site1/page.php), but if I try to go to a page that should be routed to CodeIgniter (e.g., http://www.mysites.com/site1/home), I get a 404 error from CodeIgniter. It appears to be looking for method 'home' in controller 'site1' instead of controller 'home'.

So I then tried this .htaccess file in the root directory to get rid of 'site1' being passed to index.php:

RewriteBase /site1/CI/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule /site1/(?Sad.*)) index.php/$1 [L]


Unfortunately, that caused Apache to return a 404.

I even tried this in the routes.php file:

$route['site1/(:any)'] = "$1";

which sorta worked, but page links were bad.

Any ideas? Is there a 'best practice' for configuring this set up?

#2
[eluser]Otemu[/eluser]
HI,

Test without htaccess to narrow down the problem, remove htaccess from http://www.mysites.com/site1/CI/

Then try to navigate to a page that you know exists in your codeigniter site example:
http://www.mysites.com/site1/CI/index.php/home

If it works then you know it issue with your htaccess, if it doesn't then you know some other issue within codeigniter, let us know your results

#3
[eluser]Unknown[/eluser]
I finally got everything working for putting a CI application inside an existing site one folder level deep. Here is how I did it.

Folder structure used:

site1
....page1.php
....page2.php
....etc....
....CI
.........application
.........assets
.........system
site2
site3

.htaccess in site1 folder:
Code:
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond $1 !^(images|robots\.txt|assets)
RewriteRule ^(.*)$ CI/index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^assets
RewriteRule ^(.*)$ CI/$1 [L]

Note that I took the usual "index\.php" out of the third RewriteCond in the first block (because of HybridAuth and CodeIgniter routing described below). The second block is to redirect CodeIgniter page references to the assets folder to the assets folder inside the CI folder. (Otherwise I'd have to move the assets folder to the site1 folder.)


This is the base url in the config.php file:
Code:
$config['base_url'] = 'http://www.mysites.com/site1/';


I had to add the following to the routes.php file inside the CI config folder:

Code:
$route['site1/index\.php/(:any)'] = "$1";
$route['site1/(:any)'] = "$1";

Even after the Apache redirect, CI still sees "site1" in the URI and, therefore, try to load the "site1" controller which obviously doesn't exist! The second $route[] takes "site1" away.

I'm also using HybridAuth which includes "index.php" in its callbacks, so CI will see it in the URI and try to load the "index.php" controller which obviously doesn't exist either! The first $route[] takes "site1/index.php" away.

Hopes this helps someone.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.