首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Yii 模塊綁定二級域名完整配置指南

     編輯:PHP 綜合知識

核心配置原理解析

Yii 框架通過 urlManager 的路由規則實現二級域名綁定,核心在於利用 hostInfo 解析域名並映射到對應模塊。這種機制讓不同業務模塊能以獨立子域名訪問,提升項目架構清晰度與品牌辨識度。

基礎配置步驟

1. 主配置文件設置

在 protected/config/main.php 中配置 urlManager 組件:

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false, // 注意:布爾值 false 不需引號
    'urlSuffix' => '.html',
    'rules' => array(
        // 將 test.jb51.net 映射到 blog 模塊
        'https://test.jb51.net' => array('/blog', 'urlSuffix' => '', 'caseSensitive' => false),
    ),
),

2. 多控制器路由擴展

當模塊下存在多個控制器(如 CommentController)時,需添加額外路由規則:

'rules' => array(
    // 模塊首頁
    'https://test.jb51.net' => array('/blog', 'urlSuffix' => '', 'caseSensitive' => false),
    // 文章詳情頁
    'https://test.jb51.net/post-' => array('/blog/post/view', 'urlSuffix' => '.html', 'caseSensitive' => false),
    // 評論相關
    'https://test.jb51.net/comment-' => array('/blog/comment/', 'urlSuffix' => '.html', 'caseSensitive' => false),
),

訪問 https://test.jb51.net/comment-1.html 將路由到 Blog 模塊的 Comment 控制器。

本地開發環境配置

1. Hosts 文件綁定

Windows: C:\Windows\System32\drivers\etc\hosts
Linux/Mac: /etc/hosts

127.0.0.1 www.jb51.net test.jb51.net

2. Apache 虛擬主機配置

在 httpd.conf 或 httpd-vhosts.conf 中添加:

NameVirtualHost *:80
<VirtualHost *: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
    
    <Directory "E:/wamp/www/k1029">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

3. Nginx 配置示例

server {
    listen 80;
    server_name test.jb51.net;
    root /var/www/k1029;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

多二級域名批量配置

需綁定多個子域名時,只需在 rules 數組中追加規則,並在 Apache/Nginx 中添加對應虛擬主機:

'rules' => array(
    'https://blog.jb51.net' => array('/blog', 'urlSuffix' => '', 'caseSensitive' => false),
    'https://shop.jb51.net' => array('/shop', 'urlSuffix' => '', 'caseSensitive' => false),
    'https://api.jb51.net' => array('/api', 'urlSuffix' => '', 'caseSensitive' => false),
    // 更多子域名...
),

常見問題排查

  • 404 錯誤:檢查 showScriptName 是否為 false、URL 規則正則是否匹配、Apache Rewrite 模塊是否啟用
  • 樣式/JS 失效:確保資源路徑使用絕對路徑或正確的相對路徑
  • Session 跨域共享:在 main.php 中設置 'session' => array('cookieParams' => array('domain' => '.jb51.net'))
  • HTTPS 支持:規則中使用 https:// 協議,並配置 SSL 證書

生產環境部署建議

  1. DNS 解析添加泛域名記錄 *.jb51.net 指向服務器 IP
  2. 配置 SSL 證書支持 HTTPS 訪問
  3. 啟用 CDN 加速靜態資源加載
  4. 設置監控告警,及時發現子域名訪問異常
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved