首页 / 网页编程 / PHP / PHP Yii开源框架入门学习(二)修改Yii网站访问路径
PHP Yii开源框架入门学习(二)修改Yii网站访问路径2013-11-19默认网站访问路径如下所示:http://127.0.0.1:8080/zuizen/index.php?r=admin/UserInfo/admin这种路径对搜索引擎不友好,需要改成如下形式:http://127.0.0.1:8080/zuizen/admin/UserInfo/admin.html以下步骤实现以上要求:1) 修改Apache配置,使其支持重写:打开Apache配置文件httpd.conf:开启apache的mod_rewrite模块:去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符号确保<Directory "D:/var/www/html"></Directory>中有“AllowOverride All”重启Apache。2) 修改Yii网站配置:在项目中的/protected/config/main.php中找到components 下的urlManager,将其修改为: "urlManager"=>array( "urlFormat"=>"path", "rules"=>array(), "showScriptName"=>false, "urlSuffix"=>".html", ),urlFormat设置path:默认值为get,即在url中通过get参数r来表示请求的资源(/path/to/EntryScript.php?name1=value1&name2=value2...)。path则通过路径形式表示:( /path/to/EntryScript.php/name1/value1/name2/value2...)。showScriptName设置为false:在url 中不出现入口文件“/index.php”,此时需要设置web 服务器的转发规则,将不能明确资源位置的请求均转发至入口文件。rules设置了action的参数映射模式,用正则表达式来表示,具体参阅 CUrlManager3) 为网站添加重写权限:在与网站根目录index.php文件同级目录下添加文件“.htaccess”,内容如下:Options +FollowSymLinksIndexIgnore */*RewriteEngine on# if a directory or a file exists, use it directlyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# otherwise forward it to index.phpRewriteRule . index.php4) 现在即可使用所需路径格式访问了。