Yii Url优化方案、中文提示
2017-08-07 本文已影响12人
v1i555
虽然Yii2没有自带优雅的URL地址,但是它提供了可以方便优化的URL方案,使用类似于兼容模式的URL地址,可以很方便的对其进行重写,达到优化的目的
需求:
将 http://www.yii2.com/index.php?r=site/index 优化成 http://www.yii.com/site/index.html
在Yii2中配置:
'urlManager'=>[
'class' => 'yii\web\UrlManager', //指定实现类
'enablePrettyUrl' => true, // 开启URL美化
'showScriptName' => false, // 是否显示index.php
'suffix' => '.html', // 伪静态后缀
'rules'=>[
// 自定义路由规则
],
],
![](https://img.haomeiwen.com/i7252367/c476360763fefe89.png)
配置.htaccess文件
要使用.htaccess配置,必须打开分布式配置文件。
在Apache配置文件中设置(httpd.conf)
image.png
AllowOverride All
image.png
开启rewrite 模块
image.png 重启Apache
在index.php同级目录下,创建 .htaccess 文件。写入一下内容
Options +FollowSymLinks
IndexIgnore /
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
image.png
此时通过 Url::to()生成的URL就是:
![](https://img.haomeiwen.com/i7252367/54f04662a9227f8b.png)
提示中文信息
Paste_Image.png