How to make CodeIgniter accept "query string" URLs?
For reliable use of query strings I've found you need to do 3 things
- In
application/config/config.php
set$config['enable_query_strings'] = true;
- Again in
application/config/config.php
set$config['uri_protocol'] = "PATH_INFO";
- Change your .htaccess to remove the ? (if present) in the rewrite rule
I use the following
RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php/$1 [L]
//Add this method to your (base) controller :protected function getQueryStringParams() { parse_str($_SERVER['QUERY_STRING'], $params); return $params;}// Example : instagram callback actionpublic function callback(){ $params = $this->getQueryStringParams(); $code = !empty($params['code']) ? $params['code'] : ''; if (!empty($code)) { $auth_response = $this->instagram_api->authorize($code); // .... } // .... handle error}
This might help some people; put this into your controller's constructor to repopulate $_GET on a controller-by-controller basis (e.g. if you are integrating a third party lib that relies on $_GET - such as most PHP OAuth libraries).
parse_str(str_replace($_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']),$_GET);