yii2 url地址的重写与美化

PHP piniu 898浏览 0评论

有时我们想将请求的url,使用路径模式来访问。那么我们需要做如下操作

一、开启apache的 mod_rewrite 模块,在apache的配置文件中
1. 去掉LoadModule rewrite_module modules/mod_rewrite.so 前的“#”符号;

2. 把web目录区块下的 AllowOverride None 修改为 AllowOverride All;

配置完后,记得重启apache
二、在入口文件所在的目录下,创建一个.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

三、修改yii的配置文件, 本人的是 “项目名/config/web.php” 添加或者修改如下代码:

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
	
		.....
 
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => [
//                'tests/<name:\w+>' => 'backend/home/test', //模式路由
//                [ //配置数组
//                    'pattern' => 'posts',
//                    'route' => 'backend/home/login',
//                    'suffix' => '.json'
//                ],
//                '/blogs' => '/blog/index',
//                "<controller:\w+>/<action:\w+>" => "<controller>/<action>",
 
            ],
        ],
 
 
    ],
	
	.....
	
];

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • * 昵称:
  • * 邮箱: