Codeigniter routing using base_url

2.3k views Asked by At

I am using the Pagination library in Codeigniter, and the links it creates are exactly what I'm looking for. When I click on the link to go to the next page I get a 404.

My base url (index page) is: http://localhost/foo

My routing is:

$route['default_controller'] = 'main';
$route['page/(:num)'] = 'main/page/$1';

I want to be able to go to the url: http://localhost/foo/page/2 to get the second page. main is the controller. page is a function inside main.

I can type in the url http://localhost/foo/index.php/main/page/2 and get to the correct page, but I do not want to have the index.php/main in the url.

What am I doing wrong?

2

There are 2 answers

0
dnetix On BEST ANSWER

You aren't doing anything wrong. If you want to remove the index.php from the url:

Leave in blank $config['index_page'] attibute on the application/config/config.php file and make sure that you have a .htaccess that contains

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Also make sure the apache server has mod_rewrite enabled and in the configuration of apache AllowOverride attribute it's been set to All

0
Abdulla Nilam On

In you Controller(In pagination code) $config['base_url'] should be

$config['base_url'] = base_url() . 'foo/page/';

In routes.php

$route['page/(:num)'] = 'main/page/$1';
$route['page'] = 'main/page';//add this line

In .htaccess,(Place outside application folder)

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

Note: To use base_url() in config/autoload.php

$autoload['helper'] = array('url');//url

and in config/config.php, (Change this)

$config['base_url']   = '';
$config['index_page'] = '';