yii2美化url需要如下几步:
在配置文件中(默认情况下在conf/web.php中)
修改如下:
'components'=> [ .... 'urlManager' => [ 'enablePrettyUrl' => true, //启用美化url 'showScriptName' => false, // 在URL路径中是否显示脚本入口文件 'rules' => [ /*'<controller:\w+>/view/<slug:[\w-]+>' => '<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', '<controller:\w+>/cat/<slug:[\w-]+>' => '<controller>/cat',*/ ], ], ..... ]
在入口文件(index.php)所在的目录建立 .htaccess
并输入如下的内容(将Url中目录不存在的都指向到index.php文件)
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