当我将welcome设置为默认控制器时,路由不起作用并且默认控制器显示错误404,然后使用welcome控制器定义的所有路由都起作用,但我的其他路由和url不起作用。
$route['default_controller'] = "welcome";
$route['testRoute'] = 'welcome/test';
$route['testRoute/(:num)'] = 'welcome/test/$i';
以上所有路线仅适用于欢迎控制器。
$route['default_controller'] = "login";
$route['loginMe'] = 'login/loginMe';
$route['logout'] = 'user/logout';
所有控制器和功能均显示错误 404。
如果你把index.php放在域名后面,这就会起作用。
http://www.example.com/index.php/login/
您应该通过替换 config 和 .htaccess 来从 url 中删除 index.php
替换config.php
$config['index_page'] = 'index.php';
由
$config['index_page'] = '';
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$0 [L]
这对我有用:
$config['enable_query_strings'] = FALSE;
$config['uri_protocol'] = 'AUTO';
Use $config['index_page'] = '';
$config['base_url'] = 'http://localhost/code';
最后使用以下htaccess文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /code/index.php/$1 [L]
</IfModule>
注意:请记住在 htaccess 文件中编辑 index.php 的路径。希望这对你有用
在主目录中创建“.htaccess”文件,其中包含以下代码。有关更多信息,请参阅 Codeigniter 3 URL 用户指南。
更多信息:https://codeigniter.com/userguide3/general/urls.html#removing-the-index-php-file
.htaccess 文件内容: 重写引擎开启 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$index.php/$1[L]