yii url隱藏.php后綴的方法:1、增加.htaccess文件;2、在config/web.php中的components數組中增加“’urlManager’ => […]”;3、修改apache即可。
本文操作環境:Windows7系統、yii2.0版、Dell G3電腦。
yii url 怎么隱藏.php后綴?
Yii 框架開啟 URL 美化,隱藏 index.php [ 2.0 版本 ]
url美化:
立即學習“PHP免費學習筆記(深入)”;
目的:將 http://localtest/yii/web/index.php?r=hello/index
美化成:http://localtest/yii/web/hello/index
這里我是用的wampserver新建了一個localtest站點(詳情可點擊這里點擊這里),并將yii的basic文件夾重新命名為yii。
對比上面的兩個地址,其實就是把index.php?r=隱藏。
這里分兩步:
1、增加.htaccess文件
在web根目錄下增加.htaccess文件,內容為:
RewriteEngine?On DirectoryIndex?index.html?index.php #?如果是一個目錄或者文件,就訪問目錄或文件 RewriteCond?%{REQUEST_FILENAME}?!-d #如果文件存在,就直接訪問文件,不進行下面的RewriteRule RewriteCond?%{REQUEST_FILENAME}?!-f RewriteRule?.?index.php
無法直接創建.htaccess,可以先創建一個txt文件,然后另存為…,保存為文件名為.htaccess,保存類型選擇所有文件即可。
2、配置config/web.php 在config/web.php中的components數組中增加這一項:
'urlManager'?=>?[ ????//?//開啟url美化 ????'enablePrettyUrl'?=>?true, ????//?//隱藏index.php ????'showScriptName'?=>?false, ????//?//禁用嚴格匹配模式 ????'enableStrictParsing'?=>?false, ????//?//url后綴名稱 ????//?'suffix'=>'.html', ????'rules'?=>?[ ????], ],
這時,可以將URL中的index.php?r=刪除,如果出現404報錯,可以查看服務器的配置,我用的是phpstudy中集成的apache 需要檢查一下配置
confhttpd.conf中,開啟 apache 的 mod_rewrite 模塊
去掉 LoadModule rewrite_module modules/mod_rewrite.so 前的“#”符號;
然后修改 apache 的 AllowOverride
將 AllowOverride None 修改為 AllowOverride All;
由于我是在confextrahttpd-vhosts.conf中配置了站點,所以需要同步去httpd-vhosts.conf中將對應站的 AllowOverride None 修改為 AllowOverride All;
至此,我就可以用 http://localtest/yii/web/hello/index
來訪問 http://localtest/yii/web/index.php?r=hello/index
推薦:《yii教程》