使用 base_url 的 Codeigniter 路由

Codeigniter routing using base_url

我在 Codeigniter 中使用分页库,它创建的 links 正是我要找的。当我点击 link 进入下一页时,我收到了 404。

我的基础url(索引页)是:http://localhost/foo

我的路线是:

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

我希望能够转到 url: http://localhost/foo/page/2 获取第二页。 main 是控制器。 pagemain.

中的一个函数

可以输入urlhttp://localhost/foo/index.php/main/page/2并进入正确的页面,但我不会 想要 index.php/main 在 url.

我做错了什么?

你没有做错任何事。如果要从 url 中删除 index.php:

application/config/config.php[=19= 上的 $config['index_page'] 属性留空] 文件并确保您有一个包含

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

还要确保 apache 服务器已启用 mod_rewrite 并且在 apache AllowOverride 属性的配置中它已设置为 All

在你的Controller中(在分页代码中)$config['base_url']应该是

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

在routes.php

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

.htaccess中,(放在应用程序文件夹外)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/ [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'] = '';