在没有 index.php 的情况下调用控制器时 codeigniter 3 中出现 404 错误
404 error in codeigniter 3 when calling controller without index.php
我正在使用 codeigniter 3。
我像这样创建了登录控制器
<?php
class Login extends CI_Controller
{
public function index()
{
echo "It's working";
}
}
?>
我的 .htaccess 文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
我还启用了重写模块。
正在处理
http://localhost/codeigniter/index.php/login
但不在
http://localhost/codeigniter/login
我该如何解决这个问题。或其在 codeigniter 3.0.4 中的错误
请帮忙。
改变
$config['index_page'] = 'index.php';
到
$config['index_page'] = '';
在 config/config.php
打开 config.php 并执行以下替换
$config['index_page'] = "index.php"
到
$config['index_page'] = ""
在某些情况下,uri_protocol
的默认设置无法正常工作。
只需替换
$config['uri_protocol'] ="AUTO"
来自
$config['uri_protocol'] = "REQUEST_URI"
HTACCESS
RewriteEngine on
RewriteCond !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L,QSA]
您的 mod_rewrite 规则正在重定向到 /index.php 而不是 /codeigniter/index.php.
将其放入您的 .htaccess 文件中:
RewriteBase /codeigniter/
你可以试试我在 xampp.
上使用的这个 htaccess
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L,QSA]
放在项目的主目录下。
然后 config.php
$config['base_url'] = 'http://localhost/your_project/';
$config['index_page'] = '';
并确保您的 class 和文件名的首字母大写,如 here 所述。
还有更多htaccess Here
如果这仍然不起作用,您可能需要检查 httpd conf 以确保允许覆盖 .htaccess,因为 AllowOverride 控制可以放置在 .htaccess 文件中的指令。不变的是,如果您将设置放在 .htaccess 文件中并且您不为这些设置设置 AllowOverride,则默认 Apache 设置仍将 运行 并且您的 .htaccess 中的所有内容都不会被使用。例如
<VirtualHost *:80>
ServerName application.co
DocumentRoot "/Users/www/htdocs/application/"
ErrorLog "/Users/www/logs/application_error.log"
<Directory "/Users/www/htdocs/application/" >
DirectoryIndex index.php index.html
Order allow,deny
Allow from all
Allow from localhost, application.co
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
我正在使用 codeigniter 3。
我像这样创建了登录控制器
<?php
class Login extends CI_Controller
{
public function index()
{
echo "It's working";
}
}
?>
我的 .htaccess 文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
我还启用了重写模块。
正在处理
http://localhost/codeigniter/index.php/login
但不在
http://localhost/codeigniter/login
我该如何解决这个问题。或其在 codeigniter 3.0.4 中的错误 请帮忙。
改变
$config['index_page'] = 'index.php';
到
$config['index_page'] = '';
在 config/config.php
打开 config.php 并执行以下替换
$config['index_page'] = "index.php"
到
$config['index_page'] = ""
在某些情况下,uri_protocol
的默认设置无法正常工作。
只需替换
$config['uri_protocol'] ="AUTO"
来自
$config['uri_protocol'] = "REQUEST_URI"
HTACCESS
RewriteEngine on
RewriteCond !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L,QSA]
您的 mod_rewrite 规则正在重定向到 /index.php 而不是 /codeigniter/index.php.
将其放入您的 .htaccess 文件中:
RewriteBase /codeigniter/
你可以试试我在 xampp.
上使用的这个 htaccessOptions +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L,QSA]
放在项目的主目录下。
然后 config.php
$config['base_url'] = 'http://localhost/your_project/';
$config['index_page'] = '';
并确保您的 class 和文件名的首字母大写,如 here 所述。
还有更多htaccess Here
如果这仍然不起作用,您可能需要检查 httpd conf 以确保允许覆盖 .htaccess,因为 AllowOverride 控制可以放置在 .htaccess 文件中的指令。不变的是,如果您将设置放在 .htaccess 文件中并且您不为这些设置设置 AllowOverride,则默认 Apache 设置仍将 运行 并且您的 .htaccess 中的所有内容都不会被使用。例如
<VirtualHost *:80>
ServerName application.co
DocumentRoot "/Users/www/htdocs/application/"
ErrorLog "/Users/www/logs/application_error.log"
<Directory "/Users/www/htdocs/application/" >
DirectoryIndex index.php index.html
Order allow,deny
Allow from all
Allow from localhost, application.co
Require all granted
AllowOverride All
</Directory>
</VirtualHost>