Problems with routing in CodeIgniter 3

3

I am trying to execute a code in CodeIgniter on my computer under Kubuntu. To access the home ( link ) there is no problem. But when trying to access another URL ( link ) the message appears

  

"The requested URL / client was not found on this server.".

My .htaccess:

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

The routes.php file:

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['cliente'] = 'clientes';
$route['produto'] = 'produtos';

I have the files Clients.php and Produtos.php in the application / controllers folder.

In the file config.php: $config['index_page'] = '';

What is more strange is that to access the home there is no problem. Just give the error when I try any other URL type link .

The controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Clientes extends CI_Controller {


    public function __construct()
    {
        parent::__construct();
        $this->load->model('ci_sga_model');
        $this->load->library(array('session','email'));
    }

    public function index()
    {
        $this->load->view('clientes');
    }

}
    
asked by Luis Carlos Silva 03.01.2019 в 23:35
source

3 answers

0

In the root of your project you must add the following .htaccess . This file allows you to control the routing of the urls in CodeIgniter without the index.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/fechas/$1 [R=301,L]
</IfModule>

Next, you will add another .htaccess in your application folder

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

In your routes.php file you will configure the following:

...
$route['Clientes'] = 'clientes';

Please note that for some servers it is sensible to recognize uppercase and lowercase. The problem you're having is that you can not find clientes because your server is sensitive and it would be correct to specify it in routes.php % Clientes with the C capital.

The Clientes.php controller would look like this:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Clientes extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('ci_sga_model');
        $this->load->library(array('session','email'));
    }

    public function index()
    {
        $this->load->view('clientes');
    }
}

If until this point nothing works, try adding a Helper to help you in the topic of the urls. You can find more information about them in this link .

$this->load->helper('url_helper');

  

The code is added inside the controller constructor Clientes.php

    
answered by 04.01.2019 в 04:26
0

Try changing the line in .htaccess

    RewriteRule ^(.*)$ index.php?/$1 [L]

By

    RewriteRule ^(.*)$ /cliente/index.php?/$1 [L]

Alternatively you can write

    RewriteBase /cliente/
    RewriteRule ^(.*)$ index.php?/$1 [L]

The reason why it fails is that when the mod_rewrite rewrites the url it does not include the folder in which the project is located. You can add that by adding it explicitly in the rule.

    
answered by 04.01.2019 в 06:36
0

The problem was that the mod_rewrite in Apache was not activated. The solution is in this link: link Thanks for your answers.

    
answered by 04.01.2019 в 13:03