简体   繁体   English

使用base_url的Codeigniter路由

[英]Codeigniter routing using base_url

I am using the Pagination library in Codeigniter, and the links it creates are exactly what I'm looking for. 我正在使用Codeigniter中的分页库,它创建的链接正是我要寻找的。 When I click on the link to go to the next page I get a 404. 当我单击链接转到下一页时,我得到404。

My base url (index page) is: http://localhost/foo 我的基本网址(索引页)是: 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. 我希望能够转到URL: http://localhost/foo/page/2来获取第二页。 main is the controller. main是控制器。 page is a function inside main . pagemain内部的函数。

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

What am I doing wrong? 我究竟做错了什么?

You aren't doing anything wrong. 你没有做错任何事。 If you want to remove the index.php from the url: 如果要从网址中删除index.php:

Leave in blank $config['index_page'] attibute on the application/config/config.php file and make sure that you have a .htaccess that contains 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>

Also make sure the apache server has mod_rewrite enabled and in the configuration of apache AllowOverride attribute it's been set to All 还要确保apache服务器已启用mod_rewrite,并且在apache AllowOverride属性的配置中将其设置为All。

In you Controller(In pagination code) $config['base_url'] should be 在您的控制器中(在分页代码中) $config['base_url']应该是

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

In routes.php 在routes.php中

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

In .htaccess ,(Place outside application folder) .htaccess ,(放置在应用程序文件夹之外)

<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 注意:要在config/autoload.php使用base_url()

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

and in config/config.php , (Change this) 并在config/config.php ,(更改此设置)

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

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

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