标签归档:yii rewrite设置

Yii2.0 设置伪静态 / yii2.0 rewrite设置

1. 开启 apache 的 mod_rewrite 模块
打开apache/conf/httpd.conf,找到
LoadModule rewrite_module modules/mod_rewrite.so,然后去掉前的“#”符号;

2. 修改 apache httpd.conf中的 AllowOverride

把 AllowOverride None 修改为 AllowOverride All;

3. 两种方式任选其一

①在与index.php文件同级目录(web目录)下添加文件“.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

②在httpd-vhost.conf中设置

<VirtualHost *:80>
ServerName www.yii.loc
DocumentRoot “D:/xampp/htdocs/study/basic/web”
<Directory “D:/xampp/htdocs/study/basic/web”>
# 开启 mod_rewrite 用于美化 URL 功能的支持(译注:对应 pretty URL 选项)
RewriteEngine on
# 如果请求的是真实存在的文件或目录,直接访问
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 如果请求的不是真实文件或目录,分发请求至 index.php
RewriteRule . index.php
# …其它设置…
</Directory>
</VirtualHost>

4. 配置应用的urlManager

需要在config/web.php中的components数组下增加:

‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’=>false,
‘rules’ => [
// your rules go here
],
// …
],
具体可用属性,可查阅API文档。

yii2.0与之前版本配置略有不同,根据文档显示:

http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html