Search code examples
phpcodeigniterroutescodeigniter-3

Cleaner links with routing in codeigniter


I want to do a dummy Netflix clone in Codeigniter, but i have problem with routes, for example, my route looks like this:

http://localhost/NotFlix/index.php/add_page

I would like it to be nicer like:

http://localhost/NotFlix/add_page

This is my link to site in header:

<a href="./index.php/add_page">Dodaj film</a>

And this is my route file:

$route['index.php/add_page'] = 'add_page';

Solution

  • You should change .htaccess file located root folder(not in the application folder). If there is no that file then pls create the file.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php?/$0 [PT,L]
    </IfModule>
    

    After then your router

    $route['add_page'] = 'add_page';
    

    your config file

    $config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'];
    $config['index_page'] = 'index.php/';
    
    $config['uri_protocol'] = 'REQUEST_URI';
    

    Then you can use a tag like the following.

    <a href = "<?=base_url()?>add_pagex">Dodaj film</a>