yii2 urlManager url美化及apache下yii2 rewrite设置
2015-12-24 15:36
501 查看
近期在学习yii2框架的使用,在看别人的博客时讲到了美化url的实现
比如,默认进入about页面的时候,url是这样的http://localhost/index.php?r=site%2Fabout,只要在/config/web.php中,在components数组中加上
'urlManager'
=> [
'showScriptName' => false,
'enablePrettyUrl' => true
],
开启之后再访问about页面,路径已变为http://localhost/web/site/about
但是我在本机上使用的时候,一直报404错误,搜索过后才知道是因为apache没有开启rewrite功能
最后找到了这个博客
http://www.chenruixuan.com/archives/759.html
yii basic版:
1. 开启 apache 的 mod_rewrite 模块
去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符号;
2. 修改 apache 的 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
4. 配置应用的urlManager
yii2.0与之前版本配置略有不同,根据文档显示:
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
需要在config/web.php中的components数组下增加:
具体可用属性,可查阅API文档。
最后终于好使了~
比如,默认进入about页面的时候,url是这样的http://localhost/index.php?r=site%2Fabout,只要在/config/web.php中,在components数组中加上
'urlManager'
=> [
'showScriptName' => false,
'enablePrettyUrl' => true
],
开启之后再访问about页面,路径已变为http://localhost/web/site/about
但是我在本机上使用的时候,一直报404错误,搜索过后才知道是因为apache没有开启rewrite功能
最后找到了这个博客
http://www.chenruixuan.com/archives/759.html
yii basic版:
1. 开启 apache 的 mod_rewrite 模块
去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符号;
2. 修改 apache 的 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
4. 配置应用的urlManager
yii2.0与之前版本配置略有不同,根据文档显示:
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
需要在config/web.php中的components数组下增加:
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName'=>false, 'rules' => [ // your rules go here ], // ... ],
具体可用属性,可查阅API文档。
最后终于好使了~
相关文章推荐
- Apache Isis 1.4.0 发布,领域驱动开发框架
- Linux快速构建apache web服务器
- Awstats处理多apache日志
- 安装perl模块小窍门
- PHP+Apache在Windows 9x下的安装和配置
- Apache服务器配置全攻略
- Apache Web让JSP“动”起来
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- wordpress rewrite问题解决
- apache 环境下 php 的配置注意事项
- 在RedHat下安装apache jserv 1.1.2方法
- windows8.1下Apache+Php+MySQL配置步骤
- Yii PHP Framework实用入门教程(详细介绍)
- windows中PHP5.2.14以及apache2.2.16安装配置方法第1/2页
- 在Windows XP下安装Apache+MySQL+PHP环境
- 比较详细的Asp伪静态化方法及Asp静态化探讨
- 解析阿里云ubuntu12.04环境下配置Apache+PHP+PHPmyadmin+MYsql
- apache rewrite防盗链三例
- ISAPI-REWRITE伪静态规则写法以及说明