q987654 发表于 2018-12-21 06:14:49

CodeIgniter框架去掉url中的index.php

CodeIgniter框架,PHP开发中的一款MVC模式框架。去掉url中的index.php,需要对url进行重写。这个重写功能是由Apache服务器提供的,实际上不知框架的url可以从重写,任何一个项目的url都可以被重写。只要利用Apache服务器的重写规则。

下面就针对CodeIgniter框架去掉url中的index.php来具体说说步骤:




[*](1)加载mod_rewrite.so模块,在httpd.conf中,将“LoadModule rewrite_module modules/mod_rewrite.so”前面的“#”去掉
[*](2)开启url重写功能。在httpd.conf文件中加上:
[*]
[*]    Options Indexes FollowSymLinks
[*]    AllowOverride All
[*]    Order allow,deny
[*]    Allow from all
[*]




这个是为了让Apache支持D:/phpserver/www/CodeIgniter文件夹下的.htaccess文件(具体目录要换成实际项目中的根目录)

(3)在D:/phpserver/www/CodeIgniter文件夹下加入.htaccess文件(注意前面的小数点),文件内容如下:

RewriteEngine on   

RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)   //上面的代码意思是排除某些目录或文件,使得这些目录不会 rewrite 到 index.php 上,这一般用在图片、js、css 等外部资源上。也就是说非 PHP 代码都要排除出去。

RewriteRule ^(.*)$ /CodeIgniter/index.php/$1

(4)修改还要修改 config.php 这个文件中的下列内容:

$config['index_page'] = 'index.php'

改为

$config['index_page'] = ''




这样就可以在url中直接写Controller和Function名,而不用加上index.php了











页: [1]
查看完整版本: CodeIgniter框架去掉url中的index.php