'components'=>[
'urlManager'=>[
'urlFormat'=>'path', // 路徑格式:/controller/action/param/value
'showScriptName'=>false, // 隱藏 index.php
'urlSuffix'=>'.html', // 可選:偽靜態後綴
'caseSensitive'=>false, // 不區分大小寫
'rules'=>[
'posts'=>'post/list',
'post/'=>'post/view',
'post//'=>'post/view',
'/'=>'/',
],
],
],
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
確保 Apache 載入 mod_rewrite 並允許 AllowOverride All。
| 規則 | 匹配 URL | 路由到 |
|---|---|---|
'posts'=>'post/list' | /posts.html | PostController::actionList() |
'post/ | /post/123.html | actionView($id=123) |
'post/ | /post/123/hello-world.html | actionView($id=123, $title='hello-world') |
' | /user/login.html | UserController::actionLogin() |
'tos'=>['site/page', 'defaultParams'=>['alias'=>'terms']] 訪問 /tos.html 等同 /site/page/alias/terms.html。'post/'=>['post/view', 'urlSuffix'=>'.json'] 單獨指定 JSON 後綴。'https://.example.com/' => '/host' 支持二級域名路由。actionView($id) 自動綁定,或 Yii::app()->request->getQuery('id')。規則過多會降低解析速度(線性匹配)。高流量站點建議: