.htaccess 默认的CodeIgniter控制器不工作还是我的htaccess?

5anewei6  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(134)

我正在使用CodeIgniter 3.1.13,并配置了我的htaccess文件来删除“index.php”,但是当我试图访问一个网页时,它不起作用,除非我在URL中放置默认控制器。
转到以下URL可以正常工作...

但以下URL不起作用...

如何使此URL正常工作?
这是我的htaccess文件...

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

#force https
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
</IfModule>

<IfModule !mod_rewrite.c>

# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php

</IfModule>

我把我的config.php改成了这个...

$config['base_url'] = 'https://www.example.com/';
$config['index_page'] = '';

这是我的Welcome.php控制器...

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

function __construct()
{   
    parent::__construct();
}

function index()
{
    $this->load->view('homepage', $data);
}

function page($pageName)
{
    $data['content'] = $this->load->view($pageName, '', TRUE);
    $this->load->view('template_page', $data);
}

}
v8wbuo2f

v8wbuo2f1#

$route['default_controller']仅指定当请求网站的根目录时执行哪个控制器。即:当没有给定controller/method子目录/string时。在您的示例中,这将是当请求www.example.comwww.example.com/index.php时。
当请求的url子目录/string以其方法之一开头时,默认控制器不会执行。
要使www.example.com/page/test url正常工作,您需要有一个Page控制器,其中包含一个test方法:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends CI_Controller {

    function __construct()
    {   
        parent::__construct();
    }

    function test()
    {
        $this->load->view('test');
    }

}

或者,将以下内容添加到config/routes.php中,以使Welcome控制器的page方法处理www.example.com/page/* URL:

$route['page/(:any)'] = 'welcome/page/$1';

相关问题