yii框架美化访问路径,去掉index.php/?r=部分

一、找到配置文件(ps:advance高级模板)
在工程目录-> backend目录 或 frontend目录 -> config目录 -> main.php文件
-> 在 return 数组下 找到这样一个属性数组开始更改吧
 
二、目的:我只想去掉浏览器地址栏中的 index.php?r= 这一块。
1、配置文件
'urlManager' => [
'enablePrettyUrl' => true, //true:美化的url,可以去掉?r=
'showScriptName' => false, //false:隐藏index.php
'suffix' => '.html', //后缀,如果设置了此项,那么浏览器地址栏就必须带上.html后缀(加载控制器方法的后面),否则会报404错误
'rules' => [
//设置规则:待续......
],
],
 
2、后续工作
改完这些还没有结束
我们可以这样访问了 http://localhost/yii_v3/backend/web/index.php/site/login.html
 
改了以上这些,我发现?r=这块可以用/代替访问了,但是想隐藏掉index.php还是不行。
我们还需在index.php同级的目录下,也就是/web目录下,添加.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
 
也可以是这样(thinkphp中 .htaccess的内容)
 
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
 
3、报错信息
没有.htaccess 文件回报这样的错误
 
The requested URL /yii_v3/backend/web/site/login.html was not found on this server.
 
4、测试成功
最后测试OK了!
 
5、示例解释
访问路径示例:http://localhost/yii_v3/backend/web/site/login.html
 
解释:
localhost:本地服务器地址
yii_v3:工程目录
backend:工程下的后台目录
web:backend下的web目录
site:控制器
login:控制器方法
.html:后缀
 
6、小小提醒
上面的步骤都完成,我们可以美化的输入地址访问了,心情很美丽
 
不过还可以这样访问是没有问题的注意红色标记的位置
①http://localhost/yii_v3/backend/web/index.php/site/login.html ( 此处的index.php会保留,不影响访问 )
 
②http://localhost/yii_v3/backend/web/?r=site/login.html ( 访问以后,框架会自动抹掉 ?r= ,可以继续访问)
 
③http://localhost/yii_v3/backend/web/index.php/?r=site/login.html ( 访问以后,框架会自动抹掉 ?r= ,但是index.php会保留 , 可以继续访问)
 
④http://localhost/yii_v3/backend/web/site/login (注意这里没有 .html 后缀,报错 not found 404,页面找不到)
 
原文地址:https://www.cnblogs.com/guoyinli/p/6692142.html