1、basic版本:在config/web.PHP中添加
代码语言:javascript复制'urlManager' => [
'enablePrettyUrl' => true, //是否启用美化url 'suffix' =>'.html', //伪静态 后缀用html对seo友好,如果启用了这个配置, //就必须添加扩展名 'showScriptName' => false, //是否显示脚本名
'rules' => [
],
],
2、advance版本:在backend/config/main.php中添加以上代码
即可实现 www.test.com/advance/backend/web/index.php/site/login这样的访问方法,再也不用加讨厌的r=site/login了
方法:
Removing index.php from URL in YiiFramework 2.0
To hide the ‘index.php’ and enable the Pretty URL in yiiframework 2.0, this post will help you. For this we have to configure the .htaccess and web.php file.
.htaccess
Please add the following lines in ‘.htaccess’ file inside the ‘web’ directory of yii2.0 application.
1 | RewriteEngine on |
---|
2 | # If a directory or a file exists, use it directly |
---|
3 | RewriteCond %{REQUEST_FILENAME} !-f |
---|
4 | RewriteCond %{REQUEST_FILENAME} !-d |
---|
5 | # Otherwise forward it to index.php |
---|
6 | RewriteRule . index.php |
---|
Configuration of Web.php File
By default ‘config/web.php’ file does not have a option ‘urlManager’. If we want to enable a pretty url, We have to add and configure the ‘urlManager’ in ‘web.php’ file. To remove the ‘index.php’ from url, we have to the ‘showScriptName’ value as false. To remove the ‘r’ route variable from url, set the ‘enablePrettyUrl’ value as true.
01 |
---|
02 | 'urlManager' => [ |
---|
03 | 'class' => 'yiiwebUrlManager', |
---|
04 | // Disable index.php |
---|
05 | 'showScriptName' => false, |
---|
06 | // Disable r= routes |
---|
07 | 'enablePrettyUrl' => true, |
---|
08 | 'rules' => array( |
---|
09 |
---|
10 | ), |
---|
11 | ], |
---|