YII2美化url,
打开 config\web.php, 在 components 这个大数组里面添加以下内容
‘urlManager’ => [
// 路由路径化
‘enablePrettyUrl’ => true,
// 隐藏入口脚本
‘showScriptName’ => false,
// 假后缀
‘suffix’=>’.html’,
‘rules’ => [
‘<controller:\w+>/<action:\w+>’=>'<controller>/<action>’,
],
],
注意上面的代码复制粘贴的单引号变化情况,可能要修改一下。
添加完,在 /web 目录下新建文件 .htaccess,写入以下内容
<IfModule rewrite_module>
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
当然 apache 开启 rewrite
nginx 配置,在location / 里加入
location / {
if (!-e $request_filename){
rewrite ^/(.*) /index.php?r=$1 last;
}
}
收工