簡體   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