本来访问路由框架的url是:php
http://www.myyii.com/web/index.php?r=hello/list;html
可是这样的路由对于SEO太过于不友好;web
第一步:去掉路由中的webapache
解决办法:配置虚拟主机的时候,将虚拟主机的根目录直接指向到项目的web目录下便可;致此,路由变成了 http://www.myyii.com/index.php?r=hello/list。框架
第二部:去掉路(隐藏)由中的index.phpyii
解决办法:性能
1.开启apache配置的路由重写模块;优化
2.修改urlManager组件中的'showScriptName'配置项:url
'showScriptName' => false, //隐藏入口脚本htm
此时,路由变成了 http://www.myyii.com/?r=hello/list;
第三部分:自定义路由规则,实现完全优化
解决办法:
修改urlManager组件中的'rules'配置项,在其中定义本身想要的路由规则;如:
'urlManager' => [
//'suffix' => '.html', //假后缀
'enablePrettyUrl' => true, //路由路径化
'showScriptName' => false, //隐藏入口脚本
'rules' => [
'list' => 'hello/list', /没有参数
'list/<p:\d+>' => 'hello/list',//有一个参数p
'test/<p:\d+>/<id:\d+>/<test:\w+>' => 'hello/test',//有三个参数p、id、test
],
],
注:
1. .htaccess文件是跟随入口文件,必须和入口文件处于同一级目录;
2. .htaccess 当访问量高了之后会很影响性能:放在文件中读取的是磁盘,放在配置文件中读取的是内存;如:
<VirtualHost *:80>
ServerAdmin xiongjiangbo
DocumentRoot "d:/workFile/myyii/web/"
ServerName www.myyii.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
<Directory "d:/workFile/myyii/web/">
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </Directory> </VirtualHost>