找到backend/config/main.php文件,’components'=>[] 中添加如下代碼:
框架:Yii2.0 advanced框架
以修改後臺文件爲例,找到backend/config/main.php文件,’components'=>[] 中添加如下代碼:
1 'urlManager' => [
2 'enablePrettyUrl' => true,
3 'showScriptName' => false,
4 'enableStrictParsing' => false,
5 'suffix' => '.html',
6 'rules' => [
7 '/'=>'/',
8 ],
9 ],
修改前:index.php?r=site/index
修改後:index.php/site/index
接下來我們逐條解釋:
'enablePrettyUrl' => true, //是否開啓美化效果
'enableStrictParsing' => false, //是否開啓嚴格解析路由
什麼意思呢?假設我們在開啓了路由美化的功能(enablePrettyUrl設置爲true),enableStrictParsing設置爲false的情況下,我們直接訪問/index.php/site/index肯定是沒有問題的。但是如果我們直接訪問/index.php?r=site/index呢?發現頁面展示的效果是/site/index(實際上這裏展示的默認的路由頁面),並非是博客列表頁面。如果我們把enableStrictParsing參數設置爲true你會發現頁面直接拋出404。因爲這貨設置爲true之後,會匹配rules選項中設定的至少一個規則,那我們這裏先設置一個路由,不然沒法繼續了。
'showScriptName' => false, //是否或略腳本名index.php
而我們可以看到,現在的index.php還是沒有在url中消失,是因爲我們還缺少一個.htaccess文件
我們只要在與入口文件index.php同級的目錄下,保存一個名爲.htaccess文件,就可以隱藏index.php文件啦!
這個文件不能少 apache配製,nginx的在niginx的配製文件裏配
.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
最後測試OK了!
還不知道的加我QQ 156356969