官网给的访问数据库读取数据并显示的例子:
http://codeigniter.org.cn/user_guide/tutorial/news_section.html
按照例子增加代码完成后,访问url : www.xxx.com/index.php/news
明明访问的是news/view,每次都还得带上index.php,看着就蛋疼。。。。。。
因此有了本文: 移除 URL 中的 index.php
参照官网:
CodeIgniter URL
http://codeigniter.org.cn/user_guide/general/urls.html
Apache 服务器启用
mod_rewrite ,网址根目录下增加 .htaccess文件再加上一些简单的规则就可以移除 index.php 了。下面是官网给的一个例子,其中使用了 "否定条件" 来排除某些不需要重定向的项目:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
在上面的例子中,除已存在的目录和文件,其他的 HTTP 请求都会经过你的 index.php 文件。
注解:这些规则并不是对所有 Web 服务器都有效。
注解:确保使用上面的规则排除掉你希望能直接访问到的资源。
注解:去掉apache配置文件中的LoadModule rewrite_module modules/mod_rewrite.so的#,就打开了
mod_rewrite。
按照codeigniter官网说的修改完毕,重启 apache,然后访问url:www.xxx.com/news
然后渣渣的提示
Not Found。。。。。。。 吐你一脸
妹的,光写了 .htaccess文件, .htaccess文件不起作用,玩个毛线啊。。。。。。。。。
继续修改apache的配置文件,将网站对应的目录下的
AllowOverride设置为All
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
# AllowOverride None
# 允许.htaccess文件起作用
AllowOverride All