繁体   English   中英

使用base_url的Codeigniter路由

[英]Codeigniter routing using base_url

我正在使用Codeigniter中的分页库,它创建的链接正是我要寻找的。 当我单击链接转到下一页时,我得到404。

我的基本网址(索引页)是: http://localhost/foo

我的路线是:

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

我希望能够转到URL: http://localhost/foo/page/2来获取第二页。 main是控制器。 pagemain内部的函数。

可以输入URL http://localhost/foo/index.php/main/page/2并进入正确的页面,但我不想在URL中包含index.php/main

我究竟做错了什么?

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

application / config / config.php文件上保留空白的$ 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。

在您的控制器中(在分页代码中) $config['base_url']应该是

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

在routes.php中

$route['page/(:num)'] = 'main/page/$1';
$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/$1 [L] 
</IfModule>

注意:要在config/autoload.php使用base_url()

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

并在config/config.php ,(更改此设置)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM