Yii 的 urlManager 支援基於域名的路由規則,可輕松實現模塊綁定二級域名。本文涵蓋配置、多控制器規則、本地 DNS 及 Apache 虛擬主機完整部署流程。
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'urlSuffix' => '.html',
'rules' => array(
'https://test.jb51.net' => array('/blog', 'urlSuffix' => '', 'caseSensitive' => false),
),
),
https://test.jb51.net:目標二級域名/blog:對應模塊 IDurlSuffix => '':覆蓋全局後綴caseSensitive => false:不區分大小寫若模塊下有 CommentController,需額外添加規則:
'rules' => array(
'https://test.jb51.net' => array('/blog', 'urlSuffix' => '', 'caseSensitive' => false),
'https://test.jb51.net/comment-' => array('/blog/comment/', 'urlSuffix' => '.html', 'caseSensitive' => false),
),
訪問 https://test.jb51.net/comment-1.html 將路由到 BlogModule/CommentController。
127.0.0.1 www.jb51.net test.jb51.net
路徑:C:\Windows\System32\drivers\etc\hosts(需管理員權限編輯)。
httpd.conf 或 httpd-vhosts.conf)NameVirtualHost *:80
ServerAdmin admin@jb51.net
DocumentRoot "E:/wamp/www/k1029"
ServerName test.jb51.net
ErrorLog "logs/test.jb51.net-error_log"
CustomLog "logs/test.jb51.net-access_log" common
多個二級域名重複添加 VirtualHost 塊即可。
server_name test.jb51.net; 同理urlManager 規則正確;驗證 DNS 解析已生效。urlManager 規則寫法,關鍵在於域名作為規則鍵名。