Making CodeIgniter 2.0′s RewriteRule and PATH_INFO work on Site5

by koesbong on March 30, 2011

I wrote a web application in CodeIgniter and needed to use the combination of query strings and URI segments for the URL and this Stack Overflow post came in very handy. One thing that I changed was that I removed “parent::_sanitize_globals();” from MY_Input.php because that will break CI 2.0′s CSRF protection.

Anyways, things were going well on my local development environment until I deployed it to Site5. It didn’t matter which link I clicked on, it always redirected me back to the home view. For the longest time, I thought there was something wrong with the .htaccess file, but none of the solutions Josh Cody or I came up with worked.

I then spent some time googling and somehow stumbled upon this on CodeIgniter’s wiki. It basically said that the RewriteRule in .htaccess will only work if you use “REQUEST_URI” as the value for $config['uri_protocol'], which I thought was very weird. I contacted Site5 about this and they have no idea why that is either.

So now, it is almost not an option to remove the URI segment and query strings support from this application because of how far along this project is, so I asked Nathan Ostgard to see if there is anything Josh and I missed or how I can get around Site5′s restriction. Lo and behold, he saved the day (and he has saved many of my days and nights).

Here is the workaround that you can use to make your RewriteRule work even though you don’t use REQUEST_URI for $config['uri_protocol'] in Site5.

/*
|----------------------------------------------------------------------
| URI PROTOCOL
|----------------------------------------------------------------------
|
| This item determines which server global should 
| be used to retrieve the URI string.  The default 
| setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of 
| the other delicious flavors:
|
| 'AUTO'			  Default - auto detects
| 'PATH_INFO'		  Uses the PATH_INFO
| 'QUERY_STRING'	  Uses the QUERY_STRING
| 'REQUEST_URI'	  Uses the REQUEST_URI
| 'ORIG_PATH_INFO'	  Uses the ORIG_PATH_INFO
|
*/
if (empty($_SERVER['PATH_INFO'])) {
    $pathInfo = $_SERVER['REQUEST_URI'];
    $index = strpos($pathInfo, '?');
    if ($index !== false) {
        $pathInfo = substr($pathInfo, 0, $index);
    }
    $_SERVER['PATH_INFO'] = $pathInfo;
}

$config['uri_protocol']	= 'PATH_INFO';

And one last thing, you should only add the chunk of code from line 19-26 in your site5 environment, not your local environment.

That’s it!

5 comments

Man you just saved my day!!!!

I’ve been using ORIG_PATH_INFO to overcome this issue but it didn’t work on IE. This works pefectly! thank you for finding this. BTW I am using codeigniter 1.7.x

by Aftab on May 6, 2011 at 4:09 am. Reply #

Words cannot express. This was one of those that was really getting to me… I’ve tried suggestions from 5 diff blogs and 700 diff forums. But you had the one true answer above all answers! Awesome. Thanks.

by Ryan Duffy on July 13, 2011 at 3:44 pm. Reply #

you bet! glad it helped.

by koesbong on July 13, 2011 at 3:49 pm. Reply #

Thanks alot, it saved me. I cannot express my thanks in words,I also searched a lot for this

by vishal on August 17, 2011 at 4:44 am. Reply #

LIFE SAVER! This saved me HOURS of coding and would have saved me HOURS more had I found this sooner. THANK YOU!

P.S. In case anyone is wondering, this must be placed in the config.php file located at /system/application/config/config.php

MUCH APPRECIATED BRO! :-)

by Nate on November 27, 2012 at 12:09 am. Reply #

Leave your comment

Required.

Required. Not published.

If you have one.