繁体   English   中英

在默认 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 page not found 错误

.htaccess文件

RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [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>

但没有任何效果..

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

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

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

尝试以下解决方案:

转到 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/$1";
$route['admin'] = 'admin/login';

第 3 步

在根文件夹中添加一个名为 .htaccess 的新文件,其中包含名为 application 和 system 的文件夹

添加以下代码并打开网站

 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}/$1 [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}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM