使用.htaccess文件删除CodeIgniter中的index.php

Sta*_*tan 6 .htaccess codeigniter codeigniter-routing

我目前正在使用CodeIgniter开发一个网站,我最近偶然发现了一个路由和.htaccess问题.

基本上,我的简单网站的结构(为简单起见,我们称之为'CIExample'项目)如下:

-Home-
关于我们 - 我们的
服务
- 新闻
- 联系我们

我使用Page控制器实现的.该控制器有5个函数调用各自的页面视图,即:

Home(),调用"Home"
About()视图,调用"关于我们"视图等等.

以前,要访问'Home',我需要输入http://localhost/CIExample/index.php/page/homeurl窗口,但在设置以下路径后,我能够删除'page'(classname)部分:

$route['default_controller'] = 'page/home';
$route['home'] = 'page/home';
$route['about'] = 'page/about';
$route['service'] = 'page/service';
$route['news'] = 'page/news';
$route['contact'] = 'page/contact';
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试删除'index.php'时,棘手的部分出现了.我希望能够通过键入来访问主页http://localhost/CIExample/home.

所以我在CI论坛/教程/堆栈溢出上做了很多搜索,并找到了.htaccess文件的一些代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L] 
Run Code Online (Sandbox Code Playgroud)

http://ellislab.com/forums/viewthread/197675/#929904
我尝试了两个代码,但没有一个工作..

http://localhost/CIExample/home将指示我找到404找不到的页面,但http://localhost/CIExample/index.php/home会工作得很好.

我想知道出现了什么错误?是我的routes.php还是.htaccess或两者兼而有之?谢谢.

注意:我还更改了'config/config.php'文件 - > $config['index_page'] = '';

编辑: 最后它的作品!在config文件夹中调整config.php文件并设置以下内容$config['uri_protocol'] = 'PATH_INFO';(来自'AUTO').

可用值:

| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO
Run Code Online (Sandbox Code Playgroud)

Dunno有什么不同,但根据http://www.jotorres.com/2011/12/removing-index-php-from-url-in-codeigniter/中的一条评论,相同的代码可能/可能不起作用生产服务器.

任何人都可以解释原因吗?

Rez*_*ani 14

您只需将其粘贴到.htaccess文件中:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

并将此替换为您的application/config/config.php文件:

$config['index_page'] = '';
Run Code Online (Sandbox Code Playgroud)


小智 2

我将这个 htaccess 用于我的所有 CodeIgniter 项目。它也支持子文件夹。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #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?/$1 [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?/$1 [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 %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [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
</IfModule>
Run Code Online (Sandbox Code Playgroud)