yii框架美化訪問路徑,去掉index.php/?r=部分

來源:https://www.cnblogs.com/guoyinli/p/6692142.html

一、找到配置文件(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了!

Yii2簡單地址美化並隱藏index.php

 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章