Yii框架设置网站伪静态的话,其实就是将当前浏览器访问的伪静态地址映射到真实的项目地址,主要操作的话是修改config/main.php文件,下面就介绍下具体操作:
<?php
$params = array_merge(
require __DIR__ . '/../../common/config/params.php',
require __DIR__ . '/../../common/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);
return [
'id' => 'app-pc',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'pc\controllers',
'components' => [
'request' => [
'csrfParam' => '_csrf-frontend',
],
'session' => [
'name' => 'app-pc',
],
'log' => [
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'api' => function () {
return new \pc\services\ApiService;
},
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'test/info/<id:\d+>' => 'test/info'
'test/info/<id:\d+>.html' => 'test/info'
],
],
],
'params' => $params,
];
由上面我们能看到,主要就是修改urlManager内的rules,一般我们在浏览器访问详情页的话大概是这样的地址:
www.test.com/test/detail?id=111
通过上述修改的话,我们的地址就变成这样:
www.test.com/test/detail/111
在rules内如果该地址符合当前的配置规则,就会真实的访问项目下TestController下的info方法,要是地址想以.html结尾的话,其实就在后面的规则后加上.html
|