[英]Codeigniter controller not accessible without index.php in URL
[英]Codeigniter call Function in Controller without index.php
我知道有很多帖子困扰着这个问题。 我尝试了一切,但没有成功。
我在Windows 10计算机上使用xampp v3.2.2。 在我的htdocs中,我有一个名为mysite的项目。 在那里,我有codeigniter 3.0.3。
config.php文件
$config['base_url'] = 'http://localhost/mysite/';
$config['index_page'] = '';
routes.php文件
$route['default_controller'] = 'CI_home';
控制器CI_home.php:
class CI_home extends CI_Controller{
function __construct() {
parent::__construct();
}
public function index($lang = ''){
$this->load->helper('url');
$this->load->helper('language');
$this->lang->load($lang, $lang);
$this->load->view('view_home');
}
}
当我致电http://localhost/mysite
,页面正确显示。
问题是,当我尝试http://localhost/mysite/en
或http://localhost/mysite/index/en
我从xampp收到404。
但是,如果我尝试http://localhost/mysite/index.php/CI_home/index/en
它可以正常工作。
我做错了什么? 如何删除“ CI_home / index”?
http://localhost/mysite/.htaccess:
.htaccess
RewriteEngine On
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
我已经检查了是否启用了mod_rewrite。
请帮我!
在此先感谢,yab86
在下面尝试这个htaccess
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
确保htaccess在您的主目录中。
然后设置您的基本网址
$config['base_url'] = 'http://localhost/yourproject/';
然后删除index.php
$config['index_page'] = '';
注意: Codeigniter 3版本区分大小写。 确保类和文件名的首字母大写 。
<?php
class Ci_home extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index($lang = '') {
$this->load->helper('url');
$this->load->helper('language');
$this->lang->load($lang, $lang);
$this->load->view('view_home');
}
}
然后确保文件名为Ci_home.php
然后,您的默认路由应为
使用默认控制器时,请确保与您选择的控制器名称相同。
$route['default_controller'] = 'ci_home';
$route['(:any)'] = 'ci_home/index/$1';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.