默认网站访问路径以下所示
:
这种路径对搜索引擎不友好,须要改为以下形式:
如下步骤实现以上要求:
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)
如今便可使用所需路径格式访问了。