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,頁面找不到)
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM