`
天梯梦
  • 浏览: 13651252 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

codeigniter:去掉 URL 中的 index.php

阅读更多

去掉 URL 中的 index.php

首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite,并且已经配置好 rewrite 相关的参数。
什么是 rewrtie 可以 Google 一下。

然后,在 CI 根目录(与index.php同级) 下新建立一个配置文件,命名为: .htaccess
在里面这样写:


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

就可以去掉 index.php 了。要注意 /index.php/$1 要根据你目录(Web 目录,比如 http://www.domain.com/index.php)的实际情况来定,比如网站根目录是 /ci/index.php 则要写成 /ci/index.php/$1

RewriteCond $1 !^(index\.php|images|robots\.txt)
 

上面的代码意思是排除某些目录或文件,使得这些目录不会 rewrite 到 index.php 上,这一般用在图片、js、css 等外部资源上。也就是说非 PHP 代码都要排除出去。(这里我排除了 images 目录和 robots.txt 文件,当然 index.php 也应该被排除)
哦,对了,还要修改 config.php 这个文件中的下列内容:

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "index.php";
 

把其中的 "index.php" 改成 "" 就可以了。


另一篇:

首先,设置apache的配置文件
httpd.conf ,该文件默认在 /apache/conf/ 目录下。
开启
mod_rewrite.so 模块,然后将需要进行rewrite的目录属性设置为 AllowOverride All
开启mod_rewrite.so模块.png

rewrite的目录属性设置为AllowOverride All.png
其次:设置Codeigniter的

config.php 文件,该文件默认在 /system/application/config 目录下。
将其中的
$config['index_page'] = "index.php" 一项改为 $config['index_page'] = "";
设置Codeigniter的config.php文件.png

最后,编写.htaccess文件,将文件放在CI目录即可。
我这里给出我的.htaccess文件内容供大家参考。

<IfModule mod_rewrite.c>
<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>

Options -Indexes
Options +FollowSymLinks

#允许解析文件中的SSI指令
Options +Includes

#定义404,500错误页面
ErrorDocument 404 /404.htm
ErrorDocument 500 /404.htm

#定义目录索引页面
DirectoryIndex index.php
order deny,allow

RewriteEngine on

#设置根目录
RewriteBase /www/ci_170/

#去掉链接地址中index.php字符串
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|images|robots\.txt)

RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>
 


注:如果没有设置根目录的话,就需要在规则的index.php前面将目录加上

给出最终效果图:

去index.php效果图.png

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics