CodeIgniter 从 url 中删除 index.php

CodeIgniter removing index.php from url

我知道这个问题已经被问过很多次了。 但是,我仍然无法摆脱 index.php url。它阻止我直接访问我的控制器。

我目前正在使用 CodeIgniter 2.2.2
以下是我的 .htaccess 文件:

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

application/config:

//  Find the below code

$config['uri_protocol'] = "AUTO"

//  Replace it as

$config['uri_protocol'] = "REQUEST_URI" 

有什么想法吗?提前感谢您的宝贵时间。

在您的 .htaccess 中使用以下代码

   DirectoryIndex index.php
    RewriteEngine on
    RewriteCond  !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/ [L,QSA]

试试这个..

RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [QSA,L]

我之前遇到过同样的问题。这就是我所做的

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

然后转到 config.php 并删除 index.php

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

这对我有用。

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

如果您使用 wamp,请确保您启用了 apache mod 重写。

这个 htaccess 适用于 wamp 或 xampp

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['index_page'] = '';

在您的 .htaccess 上

RewriteEngine On

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

$config['index_page'] = '';

对我有用

来自CI User Manual:

Index.php 文件将默认包含在您的网址中。

示例:

example.com/index.php/controller

你怎么改?

您可以使用 .htaccess 中的重写规则从 URL 中删除 index.php

来自 User Guide 的示例:

RewriteEngine on
RewriteCond  !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]

使用 "negative" 方法,其中除指定项目外的所有内容都被重定向。

最后一件事让您使用 index_page 作为空位置 application/config/config。php:

$config['index_page'] = '';

最后,如果您仍然面临问题,请将 uri_protocol AUTO 更改为 REQUEST_URI

$config['uri_protocol'] = "REQUEST_URI";

终于解决了我的问题。我想把我的答案分享给大家。 我目前正在使用 mac,我将 .htaccess 文件重命名为 htaccess.txt 并在 htaccess 中包含以下代码: 重写引擎开启

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/ [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/ [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
#RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]


</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

ErrorDocument 404 /index.php

然后通过执行以下操作确保 Apache 正在读取 htaccess.txt 文件:

  1. 在 httpd.conf

  2. 上取消注释以下行
  3. 进入 extra/httpd-default.conf 文件并更改这行代码:AccessFileName .htaccessAccessFileName htaccess.txt

  4. 重新启动 Apache 服务器

  5. 在您的配置中,确保将 'base_url' 留空(索引页仍然可以存在,没关系)。 $config['uri_protocol'] = 'REQUEST_URI';

希望这可以帮助其他人!