用户
 找回密码
 入住 CI 中国社区
搜索
查看: 17243|回复: 9
收起左侧

[中级] 有效删除URL中的index.php

[复制链接]
发表于 2013-2-6 13:07:20 | 显示全部楼层 |阅读模式
本帖最后由 Altair 于 2013-3-13 09:15 编辑

                                                                                                                                        如果你刚接触CI不久又或者刚刚研读CI的使用手册的话,关于如何有效删除URL中index.php以使URL看起来更友好美观的问题,可能是你面对的第一个较为复杂的问题!本贴不是原创,而是一个各种意见的综合!但本帖提出的解决方案可以有效解决Apache和IIS两种环境下的配置问题,同时也可以有效,避免部分虚拟LINUX虚拟主机上可能出现的no input file specified错误!如果你还在为index.php的问题而苦苦寻觅解决方案,或许这里会是你的最后一站!
                                                                        

官方解决方案
默认情况下,index.php 文件将被包含在你的 URL 中:
example.com/index.php/news/article/my_article
你可以很容易的通过 .htaccess 文件来设置一些简单的规则删除它。下面是一个例子,使用“negative”方法将非指定内容进行重定向:
HTML复制代码
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
复制代码

注意:如果你的项目不在根目录请把上面这一句改为:RewriteRule ^(.*)$ index.php/$1 [L]
在上面的例子中,可以实现任何非 index.php、images 和 robots.txt 的 HTTP 请求都被指向 index.php。
我的终极解决方案但在实践中,以上方案仅适用于与运行于Apache环境下的服务器且并不具有充分的普遍适用性!当CI程序位于非根目录或位于某些虚拟主机上时,以上解决方案会引起”404错误”或”no input file specified”等错误.百度参考过相关问题的解放方案后,找到了一种具有通用性的有效删除URL路径中index.php的方法,代码参考如下:
index位于根目录时,你可以在index.php所在的根目录里新建.htaccess文件并使用以下代码:
HTML复制代码
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
复制代码

当index.php不在根目录时,你可以在index.php所在目录里新建.htaccess文件并使用以下代码:
HTML复制代码
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /path_to_app/index.php?/$1 [L]
复制代码

注意把path_to_app换成你的index.php所在文件的目录.假设你的index.php放在根目录的tool子文件夹下,你可以这样写:
HTML复制代码
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /tool/index.php?/$1 [L]
复制代码

以上方案应该可以解决Apache环境下如何有效删除URL中index.php的问题,如果有其他问题,请评论或留言并留下一个有效的联系方式.我会尝试替您解决并将可能的解决方案及时通知您!
另外,关于IIS环境下如何有效删除URL中的index.php,可以参考这篇文章:
IIS7.5 去除 index.php web.config配置文件参考文章:如何删除url中index-php的终极解决方案


评分

参与人数 2威望 +5 收起 理由
nooneisthere123 + 3 赞一个!
xiehao + 2

查看全部评分

本帖被以下淘专辑推荐:

发表于 2013-2-7 07:33:53 | 显示全部楼层
你发错地方了
 楼主| 发表于 2013-2-7 12:11:00 | 显示全部楼层
smartweb 发表于 2013-2-7 07:33
你发错地方了

?那应该发在哪个版块比较合适呢?
发表于 2013-2-8 10:37:45 | 显示全部楼层
实话说,楼主近几天各种相关的回帖实在让我纠结要不要删。
 楼主| 发表于 2013-2-8 23:38:25 | 显示全部楼层
lamtin 发表于 2013-2-8 10:37
实话说,楼主近几天各种相关的回帖实在让我纠结要不要删。

把其他帖子下的几处回复删了也没事,不过这篇帖子还是留着吧,确实有些用.
发表于 2013-3-12 09:31:49 | 显示全部楼层
你好,我用了您的方法,但是出现了错误,页面显示不出来,错误如下:
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at postmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4 Server at localhost Port 80

另:问个问题,我在application,system文件夹下,也看到有.htaccess文件,不知道有没有影响?

我的邮箱是:xbrid@163.com
 楼主| 发表于 2013-3-13 08:15:29 | 显示全部楼层
可以把你的.htaccess文件和具体的CI目录结构贴出来吗?
发表于 2013-3-13 09:49:56 | 显示全部楼层
xbrid 发表于 2013-3-12 09:31
你好,我用了您的方法,但是出现了错误,页面显示不出来,错误如下:
Internal Server Error

只需要修改根目錄的 .htaccess ,其他文件夾下估計都是deny from all,所以不需修改,給你參考一下我的.htaccess
  1. RewriteEngine On
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteRule ^(.*)$ index.php?/$1 [L]
复制代码
发表于 2014-9-20 23:56:49 | 显示全部楼层
我报的是404错误什么原因
错误:The requested URL /lian1/news/1.html was not found on this server.
我的文件路径:http://localhost/lian1/index.php
我的重写规则是
PHP复制代码
   
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
 
复制代码

这是什么原因啊
发表于 2015-4-22 15:06:52 | 显示全部楼层
这是个好东东,备注,并在我的环境中使用成功,有NGINX下的根目录及非根目录下的去INDEX.PHP配置吗

本版积分规则