CodeIgniter URL重定向删除index.php

发布时间:2016-06-16 17:26 | 人气数:1569

Apache默认情况下,index.php 文件将被包含在你的 URL 中:

example.com/index.php/class/function/ID

你可以很容易的通过 .htaccess 文件来设置一些简单的规则删除它。下面是一个例子,使用“negative”方法将非指定内容进行重定向:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d        //这两句可保证一般css、js文件正常加载。(注意删掉这句注释)
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。

Nginx下nginx.conf配置:

listen       80;
server_name  127.0.0.1 localhost ;
location / {
    root   C:/dev/www;
    index  index.php index.html index.htm;
    if (!-e $request_filename){
    rewrite ^/(.*)$ /index.php?/$1 last;
    break;
    }
}
添加 URL 后缀

通过设置 config/config.php 文件,你可以为 CodeIgniter 生成的 URL 添加一个指定的文件后缀。举例来说,如果 URL 是这样的:
example.com/index.php/products/view/shoes
你可以随意添加一个后缀,例如 .html,使其显示为:
example.com/index.php/products/view/shoes.html
(icebird注:英文中由于参数可直接看懂其含义,并未说明应修改哪个参数,在这里应修改$config['url_suffix']这个参数。)

关键词:codeigniter, url重定向, index.php