默認網站訪問路徑以下所示
:
這種路徑對搜索引擎不友好,須要改爲以下形式:
如下步驟實現以上要求:
1)
修改
Apache
配置,使其支持重寫:
打開
Apache
配置文件
httpd.conf
:開啓
apache
的
mod_rewrite
模塊:
去掉
LoadModule rewrite_module modules/mod_rewrite.so
前的「
#
」符號
確保
<Directory "D:/var/www/html"></Directory>
中有「
AllowOverride All
」
重啓
Apache
。
2)
修改
Yii
網站配置:
'urlManager'
=>
array
(
'urlFormat'
=>
'path'
,
'rules'
=>
array
(),
'showScriptName'
=>
false
,
'urlSuffix'
=>
'.html'
,
),
urlFormat
設置
path:
默認值爲
get
,即在
url
中經過
get
參數
r
來表示請求的資源(
/path/to/EntryScript.php?name1=value1&name2=value2...)
。
path
則經過路徑形式表示:
(
/path/to/EntryScript.php/name1/value1/name2/value2...
)。
showScriptName
設置爲
false
:在
url
中不出現入口文件「
/index.php
」,此時須要設置
web
服務器的轉發規則,將不能明確資源位置的請求均轉發至入口文件。
rules
設置了
action
的參數映射模式,用正則表達式來表示,具體參閱
CUrlManager
3)
爲網站添加劇寫權限:
在與網站根目錄
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
4)
如今便可使用所需路徑格式訪問了。