How to make codeigniter uri case insensitive?

Member

by jerad , in category: PHP Frameworks , 5 months ago

How to make codeigniter uri case insensitive?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 5 months ago

@jerad 

By default, CodeIgniter treats the URI (Uniform Resource Identifier) as case sensitive. If you want to make the URI case insensitive, you can follow these steps:

  1. Open the config.php file located in the application/config directory.
  2. Look for the line that defines the uri_protocol and change it to QUERY_STRING. It should look like this:
1
$config['uri_protocol'] = 'QUERY_STRING';


  1. Save the changes to the config.php file.
  2. Create a new file called MY_Router.php in the application/core directory if it doesn't already exist.
  3. Add the following code to the MY_Router.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
class MY_Router extends CI_Router {
    function __construct() {
        parent::__construct();
    }
    
    function _set_request($segments = array()) {
        $uri = implode('/', $segments);
        $uri = strtolower($uri);
        
        parent::_set_request($uri);
    }
}


  1. Save the changes to the MY_Router.php file.


With the above changes, CodeIgniter would now treat the URI as case insensitive. Keep in mind that this solution may have unintended consequences depending on your specific application requirements, so test thoroughly before deploying to a production environment.