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

去掉 URL 中的 index.php

    [复制链接]
发表于 2007-10-15 13:27:36 | 显示全部楼层 |阅读模式
首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite,并且已经配置好 rewrite 相关的参数。
什么是 rewrtie 可以 Google 一下。

然后,在 CI 根目录下新建立一个配置文件,命名为: .htaccess
在里面这样写:
  1. RewriteEngine on   
  2. RewriteCond $1 !^(index\.php|images|robots\.txt)   
  3. RewriteRule ^(.*)$ /index.php/$1 [L]
复制代码
就可以去掉 index.php 了。要注意 /index.php/$1 要根据你目录(Web 目录,比如 http://www.domain.com/index.php)的实际情况来定,比如网站根目录是 /ci/index.php 则要写成 /ci/index.php/$1
  1. RewriteCond $1 !^(index\.php|images|robots\.txt)
复制代码

上面的代码意思是排除某些目录或文件,使得这些目录不会 rewrite 到 index.php 上,这一般用在图片、js、css 等外部资源上。也就是说非 PHP 代码都要排除出去。(这里我排除了 images 目录和 robots.txt 文件,当然 index.php 也应该被排除)
哦,对了,还要修改 config.php 这个文件中的下列内容:
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" 改成 "" 就可以了。
发表于 2017-9-13 16:27:49 | 显示全部楼层
Hex 发表于 2008-2-15 13:59
是的,但也需要根据你的目录情况进行修改!
具体你应该看看 apache rewrite 的相关手册!这是 apache 的问 ...

大神 我想问一下  为什么我的
RewriteEngine on   
RewriteCond $1 !^(index.php|images|robots.txt)   
RewriteRule ^(.*)$ /index.php/$1 [L]
这么写就不行会显示 没有文件
把第三行 改成 RewriteRule ^(.*)$ /index.php?/$1 [L]
就可以了  想请教一下  是什么原因
发表于 2015-12-16 11:41:56 | 显示全部楼层
我在nginx 下既隐藏了 url地址中的index.php 同时又能引入 静态资源 css.js等文件。具体配置是
           rewrite .*/(statics|uploads)/(.*)$ /$1/$2 last;
       rewrite /.* /index.php break;
将css,js等文件放到 statics目录下,并且statics目录和system,application目录是同级别的目录
发表于 2017-9-15 09:06:26 | 显示全部楼层
这个是,我用的,根目录下的,.htaccess文件:

  1. # BEGIN CodeIgniter
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4. RewriteBase /
  5. RewriteRule ^index\.php$ - [L]
  6. RewriteCond %{REQUEST_FILENAME} !-f
  7. RewriteCond %{REQUEST_FILENAME} !-d
  8. RewriteRule . /index.php [L]
  9. </IfModule>

  10. # END CodeIgniter
复制代码
发表于 2017-11-23 10:40:31 | 显示全部楼层
大神,可以私聊吗
QQ;402519223
发表于 2017-9-15 09:06:51 | 显示全部楼层
Garrett 发表于 2017-9-13 16:27
大神 我想问一下  为什么我的
RewriteEngine on   
RewriteCond $1 !^(index.php|images|robots.txt)   

参见这个:
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 是什么意思?
https://segmentfault.com/q/1010000009402187/a-1020000009403042
 楼主| 发表于 2017-9-14 10:21:02 | 显示全部楼层
Garrett 发表于 2017-9-13 16:27
大神 我想问一下  为什么我的
RewriteEngine on   
RewriteCond $1 !^(index.php|images|robots.txt)   

你的 codeigniter config 设置的不是 PATH_INFO 吧?
发表于 2017-4-18 22:24:07 | 显示全部楼层
RewriteCond $1 !^(index\.php|images|robots\.txt)  这里的静态资源我在view中的文件里引用还是不行,我的静态资源都放在了根目录,必须要用base_url
发表于 2016-2-24 18:09:13 | 显示全部楼层
我按你的步骤尝试了下,发现好像也不行啊,我也采用的Apache服务器

本版积分规则