如何在 CodeIgniter 中处理基本 url 调用


How to handle base url calls in CodeIgniter?

>我有一个带有索引函数的默认控制器,可以使用

http://localhost/myproject/welcome

但我也想处理对基本 url 的调用

http://localhost/myproject/

我该怎么做?

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller
{
    public function index()
    {
        $this->load->view('welcome_message');
    }
}

编辑:我已将欢迎控制器声明为默认控制器

$route['default_controller'] = 'welcome';

这是 htaccess

RewriteEngine on
RewriteCond $1 !^(index'.php|resources|robots'.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

由于您似乎想从URL中删除index,因此建议您首先设置.htaccess来执行此操作:

 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} -s [OR]
 RewriteCond %{REQUEST_FILENAME} -l [OR]
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule ^.*$ - [NC,L]
 RewriteRule ^.*$ index.php [NC,L]

第二步:

检查 CI 的config.php并将$config['base_url']设置为index.php文件所在的位置(您说要http://localhost/myproject/。不要省略尾随反斜杠。

config['index_page']值设置为 ''(空)

您已经设置了默认控制器,您应该很高兴。