CI框架伪静态化配置

CI框架伪静态化配置

伪静态化,即:去掉入口的index.php, 在url后面加上 .html 后缀

CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样 http://localhost/CodeIgniter/index.php/welcome。不太好看,怎么把其中的index.php去掉呢?

开启 rewrite_module

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

  • 加载 rewrite_module

      // 打开 conf/httpd.conf 找到下面这句,去掉前面的#
      #LoadModule rewrite_module modules/mod_rewrite.so
    
  • 网站根目录添加 .htaccess
    在网站根目录添加 .htaccess内写伪静态正则代码,这个文件可以在conf/httpd.conf 中的AccessFileName .htaccess指定

    也可以直接在conf/httpd.conf中添加伪静态正则代码,然后重启apache(apache -k restart)就可以使用伪静态地址啦。

      > 如果没有安装 mod_rewrite,可以重新编译 Apache(或若为win系统建议重新安装),并在原有 configure 的内容中加入 –enable-rewrite=shared,然后再在 Apache 配置文件中加入正则代码。
    

mod_rewrite 简介和配置

Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范。平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等

mod_rewrite 规则的使用

# .htaccess:
RewriteEngine on
# 锁定域名
RewriteCond %{HTTP_HOST} !^www.iphpmysql.cn [NC]
RewriteRule ^/(.*) http:// www.iphpmysql.cn / [L]

RewriteEngine on
# RewriteRule 前面没有设置 RewriteCond 则为无条件重写
RewriteRule ^/test([0-9]*).html$ /test.php?id=$1 # test102.html -> test.php?id=102 伪静态url
RewriteRule ^/new([0-9]*)/$ /new.php?id=$1 [R]

mod_rewrite 规则修正符:

  1. R 强制外部重定向
  2. F 禁用URL,返回403HTTP状态码。
  3. G 强制URL为GONE,返回410HTTP状态码。
  4. P 强制使用代理转发。
  5. L 表明当前规则是最后一条规则,停止分析以后规则的重写。
  6. N 重新从第一条规则开始运行重写过程。
  7. C 与下一条规则关联
  8. T=MIME-type (force MIME type) 强制MIME类型
  9. NS 只用于不是内部子请求
  10. NC 不区分大小写
  11. QSA 追加请求字符串

  12. NE 不在输出转义特殊字符 eg: \%3d$1 等价于 =$1

--

# CI根目录/.htaccess :
RewriteEngine on
# 排除某些目录或文件
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt) # $1为下面RewriteRule捕获的元组?
RewriteRule ^(.*)$ /index.php/$1 [L]

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

最后还要修改 CIDirectory/config/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";
$config['index_page'] = "";

如果经过以上设置还是不成功的话就是Apache配置问题啦。

试试 httpd.conf文件中:

# AllowOverride None
# 改为
AllowOverride All
posted @ 2015-09-14 22:30  stephenykk  阅读(1198)  评论(0编辑  收藏  举报