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


目的:我只想去掉瀏覽器地址欄中的index.php?r=這一塊。

在/config/web.php中 ’components'=>[] 中添加如下代碼:

1 'urlManager' => [
2             'enablePrettyUrl' => true,
3             'showScriptName' => false,//隱藏index.php 
4             //'enableStrictParsing' => false,
5             'suffix' => '.html',//后綴,如果設置了此項,那么瀏覽器地址欄就必須帶上.html后綴,否則會報404錯誤
6             'rules' => [
7                 //'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
8             ],
9         ],

如果帶了改了后綴這一下,請記得一定要給瀏覽器地址欄的路徑最后面加上.html。

改了以上這些,我發現?r=這塊可以用/代替訪問了,但是想隱藏掉index.php還是不行。

我們還需在index.php同級的目錄下添加.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

然后保存在與入口文件index.php同級的目錄下,也就是/web目錄下,文件名自己填.htaccess,文件類型選擇 所有文件 (*.*) ,然后保存即可。

最后測試OK了!


免責聲明!

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



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