首先配置下nginx,確保能夠不使用index.php來訪問php
server{ listen 8082; server_name yii2.dev; access_log logs/yii2.access.log; error_log logs/yii2.error.log error; root /home/admin/web/nginx/html/basic/web/; location / { index index.html index.php; if (!-e $request_filename){ rewrite ^/(.*) /index.php?r=$1 last; } }
APP中配置confightml
'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => false, 'showScriptName' => false, 'rules' => [ '<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view', '<controller:\w+>/<id:\d+>' => '<controller>/view', ], ]
其中KEY的說明nginx
enablePrettyUrl |
Whether to enable pretty URLs. |
enableStrictParsing |
Whether to enable strict parsing. |
showScriptName |
Whether to show entry script name in the constructed URL. Defaults to true. This property is used only if $enablePrettyUrl is true. |
rules |
The rules for creating and parsing URLs when $enablePrettyUrl is true. This property is used only if $enablePrettyUrl is true. |
enableStrictParsing,showScriptName,rules 要生效必須設置enablePrettyUrl=true.所以要使rules配置的規則生效,須要首先設置enablePrettyUrl=true.設置完成之後按照映射規則訪問。http://yii2.dev:8082/admin/default/1 其中Module:admin controller:default action:view id:1 符合規則:'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',