在默认 var/www/html 文件夹内的实时服务器中,如果没有 index.php,codeigniter 路由将无法正常工作

codeigniter routes not working without index.php in live server inside default var/www/html folder

Codeigniter 路由适用于本地服务器。当我部署到服务器的 var/www/html 时,路由没有按预期工作。

当我在 URL 中的控制器名称前添加 index.php 时,它工作正常。

但没有 index.php,它会抛出 404 页面未找到错误

.htaccess 文件

RewriteEngine On

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

我也试过了

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /var/www/html/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

但没有任何效果..

尝试以下解决方案:

转到 application\config\config.php 文件

进行更改

$config['index_page'] = 'index.php';

$config['index_page'] = '';

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /YourCIFolderName/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /YourCIFolderName/index.php [L]
</IfModule>

第 1 步

转到application/config/config。php 找到

$config['index_page'] = 'index.php';
change it to
$config['index_page'] = '';

第 2 步

转到application/config/routes。php

评论结束后删除页面底部的所有内容并粘贴下面的代码

$route['default_controller'] = 'main'; // main is your default controller name
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = "main/";
$route['admin'] = 'admin/login';

第 3 步

在应用程序和系统等文件夹所在的根文件夹中添加一个名为 .htaccess 的新文件

添加以下代码并打开网站

 RewriteEngine On
RewriteBase /
Options All -Indexes
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
</IfModule>

# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule ^(.+?)/?$ .php [L]

Header set Access-Control-Allow-Origin "*"

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

您的服务器似乎未启用 mod_rewrite。 如果重写规则不起作用,请确保您允许在 Apache 配置文件中使用 .htaccess 文件。目录部分应包含一个 AllowOverride All 选项:

<Directory "/var/www/html">
   Options Indexes FollowSymLinks
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>

不要忘记对 Apache 配置文件的任何更改都需要重新启动服务!