phpcodeigniterquery-stringclean-urls

Codeigniter Query Strings URL Issue


I have already setup project having it's url like below.

index.php?c=controllerName&m=methodName

And parameter in above url like below.

index.php?c=controllerName&m=methodName&selDeg=manager

I want to use url like below for my newely created modules.

admin/Items/1

But want to use first type url as it is for previousely developed modules.

Is it possible to use url with index.php for old modules and without index.php for new modules.


Solution

  • You can prepare your config like this:

    $config['base_url'] = 'your_base_url';
    $config['index_page'] = ''; // empty string
    $config['enable_query_strings'] = FALSE; // keep it to default FALSE, you can use it even without enabling it here
    

    and your .htaccess like this:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    I think that is it, now you can use both segments and query_strings like this:

    http://localhost/codeigniter/?c=welcome&m=index
    http://localhost/codeigniter/welcome/index
    http://localhost/codeigniter/index.php/welcome/index
    http://localhost/codeigniter/index.php?c=welcome&m=index
    http://localhost/codeigniter/index.php/?c=welcome&m=index
    

    I've already tested both in my environment and it works just fine.


    Edit: in my codeigniter template i've extended my core router and slightly changed the _set_routing() method like this:

    Of course it is a bad practice to change the core system, so you need to extend the core router file and create MY_Router.php under application/core and make it extend CI_Router like this:

    class MY_Router extends CI_Router
    {
    protected function _set_routing()
    {
        if (file_exists(APPPATH.'config/routes.php'))
        {
            include(APPPATH.'config/routes.php');
        }
    
        if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
        {
            include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
        }
    
        if (isset($route) && is_array($route))
        {
            isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
            isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
            unset($route['default_controller'], $route['translate_uri_dashes']);
            $this->routes = $route;
        }
    
        $_c = trim($this->config->item('controller_trigger'));
        if ( ! empty($_GET[$_c]))
        {
            $this->uri->filter_uri($_GET[$_c]);
            $this->set_class($_GET[$_c]);
    
            $_f = trim($this->config->item('function_trigger'));
            if ( ! empty($_GET[$_f]))
            {
                $this->uri->filter_uri($_GET[$_f]);
                $this->set_method($_GET[$_f]);
            }
    
            $this->uri->rsegments = array(
                1 => $this->class,
                2 => $this->method
            );
        }
        else
        {
            if ($this->uri->uri_string !== '')
            {
                $this->_parse_routes();
            }
            else
            {
                $this->_set_default_controller();
            }
        }
    }
    }
    

    Now you you have the best of both worlds, you will keep your segments with its goodies untouched but will always look for get c=&m=, i hope its clear enough.

    We kept everything to default and made it check for a get request with c and m pretty like what enable_query_strings does but without enabling it and messing anything up to keep working with segments as it is..