好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

YII URL重写(开启伪静态)

目标:把这个url重写成下面那种

http://localhost/yii/index.php?r=site/login

http://localhost/yii/site/login

一、确定你的apache有rewrite模块,可以通过phpinfo()来查看。

没有怎么办?自己百度

二、编辑yii的配置文件 main.php

找到

1
2
3
4
5
6
7
8
9
10
 /*
 'urlManager'=>array(
 'urlFormat'=>'path',
 'rules'=>array(
 '<controller:\w+>/<id:\d+>'=>'<controller>/view',
 '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
 '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
 ),
 ),
 */ 

把注释去掉

现在应该可以看到效果了,但是还是有写瑕疵,我们要吧入口文件index.php去掉

三、在网站根目录新建一个文件.htaccess

1
2
3
4
5
6
7
8
9
 < IfModule rewrite_module > 
Options  + FollowSymLinks
IndexIgnore  *  /*
RewriteEngine On
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule> 

ok虽然去掉index.php也不报错,但是默认路径还是带了index.php

最后一步,更改配置文件main.php

找到

1
 'urlFormat'  =>  'path'  , 

在它下面增加

1
 'showScriptName'  =>  false  , 

大功告成!!

查看更多关于YII URL重写(开启伪静态)的详细内容...

  阅读:45次