yii url隱藏.php后綴的方法:1、增加.htaccess文件;2、在config/web.php中的components數(shù)組中增加“'urlManager' => [...]”;3、修改apache即可。
本文操作環(huán)境:Windows7系統(tǒng)、yii2.0版、Dell G3電腦。
yii url 怎么隱藏.php后綴?
Yii 框架開(kāi)啟 URL 美化,隱藏 index.php [ 2.0 版本 ]
url美化:
立即學(xué)習(xí)“PHP免費(fèi)學(xué)習(xí)筆記(深入)”;
目的:將 http://localtest/yii/web/index.php?r=hello/index
美化成:http://localtest/yii/web/hello/index
這里我是用的wampserver新建了一個(gè)localtest站點(diǎn)(詳情可點(diǎn)擊這里點(diǎn)擊這里),并將yii的basic文件夾重新命名為yii。
對(duì)比上面的兩個(gè)地址,其實(shí)就是把index.php?r=隱藏。
這里分兩步:
1、增加.htaccess文件
在web根目錄下增加.htaccess文件,內(nèi)容為:
RewriteEngine On DirectoryIndex index.html index.php # 如果是一個(gè)目錄或者文件,就訪問(wèn)目錄或文件 RewriteCond %{REQUEST_FILENAME} !-d #如果文件存在,就直接訪問(wèn)文件,不進(jìn)行下面的RewriteRule RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . index.php
無(wú)法直接創(chuàng)建.htaccess,可以先創(chuàng)建一個(gè)txt文件,然后另存為…,保存為文件名為.htaccess,保存類(lèi)型選擇所有文件即可。
2、配置config/web.php 在config/web.php中的components數(shù)組中增加這一項(xiàng):
'urlManager' => [ // //開(kāi)啟url美化 'enablePrettyUrl' => true, // //隱藏index.php 'showScriptName' => false, // //禁用嚴(yán)格匹配模式 'enableStrictParsing' => false, // //url后綴名稱(chēng) // 'suffix'=>'.html', 'rules' => [ ], ],
這時(shí),可以將URL中的index.php?r=刪除,如果出現(xiàn)404報(bào)錯(cuò),可以查看服務(wù)器的配置,我用的是phpstudy中集成的apache 需要檢查一下配置
conf\httpd.conf中,開(kāi)啟 apache 的 mod_rewrite 模塊
去掉 LoadModule rewrite_module modules/mod_rewrite.so 前的“#”符號(hào);
然后修改 apache 的 AllowOverride
將 AllowOverride None 修改為 AllowOverride All;
由于我是在conf\extra\httpd-vhosts.conf中配置了站點(diǎn),所以需要同步去httpd-vhosts.conf中將對(duì)應(yīng)站的 AllowOverride None 修改為 AllowOverride All;
至此,我就可以用 http://localtest/yii/web/hello/index
來(lái)訪問(wèn) http://localtest/yii/web/index.php?r=hello/index
推薦:《yii教程》
以上就是yii url 怎么隱藏.php后綴的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
PHP怎么學(xué)習(xí)?PHP怎么入門(mén)?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門(mén)到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)