Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 2.1.1 .htaccess issue after upgrade
#1

[eluser]kettch[/eluser]
I've had a client website using CodeIgniter 1.7 for about 6 years now with no issues. However, recently I updated the CodeIgniter framework to 2.1.1 and now the .htaccess is having issues. I've confirmed in the logs that it isn't getting to my controller so I believe this to be purely an .htaccess issue.

I've got several typical URL patterns:

Code:
mysite.com/<page name here>, i.e. mysite.com/faq  
mysite.com/admin/<action here>, i.e. mysite.com/admin/login

My .htaccess is currently:

Code:
RewriteRule ^([a-zA-Z0-9]+)$ page/id/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Now, as should be apparent, mysite.com/faq should be rewritten to mysite.com/page/id/faq, but it is throwing a 404 instead.

Just going to mysite.com successfully loads the default controller, in this case the home page or the equivalent to mysite.com/home. All of the admin urls are working perfectly. mysite.com/page/id/<page name here> is also working perfectly.

Just for comparison, the original .htaccess that has worked fine for 6 years until I upgraded CodeIgniter was:
Code:
RewriteRule ^((images|styles|scripts|documents)/*.*)$ $1 [L]
RewriteRule ^(admin/*)$ index.php/$1 [L]
RewriteRule ^([a-zA-Z0-9]+)$ page/id/$1
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

This original one has the exact same behavior as the one I am currently trying to use above.

The actual upgrade process was JUST on the CodeIgniter framework and reorganizing the files into the separate application and system folders, none of my actual custom code was touched other than the minimal changes that were required as part of the upgrade process.

I've tried about a billion different .htaccess command combos trying to get this thing to work and just can't get both the "normal" pages and the admin pages to load correctly with the same config. Ultimately my question is why did upgrading CodeIgniter magically break the URL rewriting, but I'd be more than happy to simply have this working again.

Does anyone have suggestions? I'd love another set of eyes on this.
#2

[eluser]Aken[/eluser]
I would remove the rule from the .htaccess entirely, and set up a route in CI.

Regarding the actual cause of the problem, you probably changed the $config['uri_protocol'] setting in /application/config/config.php when redoing CodeIgniter. Using your original .htaccess, changing that to "PATH_INFO" worked fine on CI 2.1.2. I would still use a route instead, though.
#3

[eluser]kettch[/eluser]
[quote author="Aken" date="1345086765"]I would remove the rule from the .htaccess entirely, and set up a route in CI.

Regarding the actual cause of the problem, you probably changed the $config['uri_protocol'] setting in /application/config/config.php when redoing CodeIgniter. Using your original .htaccess, changing that to "PATH_INFO" worked fine on CI 2.1.2. I would still use a route instead, though.[/quote]

Well, your second suggestion actually did get me working. I changed $config['uri_protocol'] from AUTO to PATH_INFO and it magically worked. I did have to add a line to handle the admin/ URL format though, so my final .htaccess is this:

Code:
RewriteRule ^(admin/*)$ index.php/$1 [L]
RewriteRule ^([a-zA-Z0-9]+)$ page/id/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]


Looking back in my SVN repo, $config['uri_protocol'] has always been AUTO and it worked fine for 6 years. I'd be curious to know what changed in CI between 1.7 and 2.1.1 to break this.

Regarding your first suggestion, after having this issue I did try adding routes (and removing the .htaccess rules of course) but adding routes to the routes.php file had zero effect on the site, it just always loaded my default controller. Am I missing something with routes that is preventing the routes from taking effect? The documentation makes it at least seem really simple and I'd rather do that if it makes dealing with URL rewriting voodoo go away.
#4

[eluser]Aken[/eluser]
Show me your routes. It is really simple - almost the same as .htaccess.

Code:
$route['URI you want to change'] = 'CI URI you want to load';

Example:

Code:
$route['([a-zA-Z0-9]+)'] = 'page/id/$1';
#5

[eluser]kettch[/eluser]
[quote author="Aken" date="1345087668"]Show me your routes. It is really simple - almost the same as .htaccess.

Code:
$route['URI you want to change'] = 'CI URI you want to load';

Example:

Code:
$route['([a-zA-Z0-9]+)'] = 'page/id/$1';
[/quote]

I had that exact same route in mine and mysite.com/faq just goes to a 404 instead of mysite.com/page/id/faq. Like this:

Code:
$route['default_controller'] = 'page';
$route['404_override'] = '';

$route['([a-zA-Z0-9]+)'] = 'page/id/$1';
#6

[eluser]Aken[/eluser]
I don't know what to tell you - removing the .htaccess rule and adding the route works perfectly fine for me. Are you sure your page/id method isn't throwing the 404?




Theme © iAndrew 2016 - Forum software by © MyBB