CodeIgniter URL

来源:互联网 发布:拼多多数据抓取 编辑:程序博客网 时间:2024/05/19 04:06
 

CI的URL默认风格是这样的example.com/index.php/news/article/my_article

index.php是所有请求的入口,index.php会根据url后面部分路由请求到不同的处理器上

为了让所有的请求都发送到index.php,你需要修改apache的rewrite规则(URL 重写规则)

如果你有apache配置文件的修改权限,直接修改httpd.conf配置文件的virtualhost的rewrite规则,这样是服务器处理效率最高的,如果没有权限,你可以修改根目录.htaccess文件。

增加如下rewrite规则

Options Includes FollowSymLinks

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

rewrite详细说明参考:

http://www.zzbaike.com/wiki/Apache/RewriteRule#RewriteRule_.E7.9A.84.E8.A7.84.E5.88.99.E4.BB.A5.E5.8F.8A.E5.8F.82.E6.95.B0.E8.AF.B4.E6.98.8E

添加上面的规则之后,所有请求都会被转发到index.php,同时,你的url中不必出现index.php了,你可以这样写url:example.com/news/article/my_article

其中news是类名、article是函数名

通过修改config/config.php这个配置文件中$config['url_suffix']这个参数,可以给url添加指定的后缀,如.html,有利于SEO。修改后的url如下:example.com/news/article/my_article.html看起来更像一个静态url。

如果你不想采用如上风格的URL,可以使用如下风格的url

example.com?c=news&m=article¶=my_aiticle

c表示controller、m表示method

需要修改 application/config/config.php 如下参数:

$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

 

友情链接:网购优惠网